ASP.NET Core C1 FlexGrid 101

This page shows how to get started with ASP.NET Core MVC's C1 FlexGrid controls.

Getting Started

Steps for getting started with the FlexGrid control in MVC applications:

  1. Create a new ASP.NET Core MVC project using the "C1 ASP.NET Core MVC Web Application" template.
  2. Add controller and corresponding view to the project.
  3. Initialize the C1 FlexGrid control in view using <c1-flex-grid></c1-flex-grid> tag.
  4. (Optional) Add some CSS to customize the FlexGrid control's appearance.

This will create a FlexGrid with default behavior, which includes automatic column generation, column sorting and reordering, editing, and clipboard support.

<!DOCTYPE html> <html> <head> </head> <body> <!-- this is the grid --> <c1-flex-grid id="gsFlexGrid" is-read-only="true" auto-generate-columns="true" allow-sorting="true"> <c1-items-source source-collection="@Model.CountryData"></c1-items-source> </c1-flex-grid> </body> </html>
/* set default grid style */ .wj-flexgrid { height: 300px; background-color: white; box-shadow: 4px 4px 10px 0px rgba(50, 50, 50, 0.75); margin-bottom: 12px; }
using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using FlexGrid101.Models; using C1.Web.Mvc.Grid; namespace FlexGrid101.Controllers { public class HomeController : Controller { public ActionResult Index() { FlexGridModel model = new FlexGridModel(); model.CountryData = Sale.GetData(500); return View(model); } } }

Result (live):

Column Definitions

The Getting Started example did not define any columns, so FlexGrid generated them automatically.

This example shows how you can define the columns using the FlexGrid's <c1-flex-grid-column></c1-flex-grid-column> tag.

Specifying the columns allows you to choose which columns to show, and in what order. This also gives you control over each column's Width, Heading, Formatting, Alignment, and other properties.

In this case, we use star sizing to set the width of the "Country" column. This tells the column to stretch to fill the available width of the grid so there is no empty space. On the "Amount" column, we set the format property to "n0", which results in numbers with thousand separators and no decimal digits. On the "Discount" column, we set the format property to "p0", which results in numbers with percentage and no decimal digits.

<c1-flex-grid id="cdInitMethod" is-read-only="true" auto-generate-columns="false" allow-sorting="true"> <c1-flex-grid-column binding="ID"></c1-flex-grid-column> <c1-flex-grid-column binding="Start" format="MMM d yy"></c1-flex-grid-column> <c1-flex-grid-column binding="End" format="HH:mm"></c1-flex-grid-column> <c1-flex-grid-column binding="Country" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount" format="n0"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount2"></c1-flex-grid-column> <c1-flex-grid-column binding="Discount" format="p0"></c1-flex-grid-column> <c1-flex-grid-column binding="Active"></c1-flex-grid-column> <c1-items-source source-collection="@Model.CountryData"></c1-items-source> </c1-flex-grid>
using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using FlexGrid101.Models; using C1.Web.Mvc.Grid; namespace FlexGrid101.Controllers { public class HomeController : Controller { public ActionResult Index() { FlexGridModel model = new FlexGridModel(); model.CountryData = Sale.GetData(500); return View(model); } } }

Result (live):

Selection Modes

By default, FlexGrid allows you to select a range of cells with the mouse or keyboard, just like Excel. The selection-mode property allows you to change that so that you can select a Row, a Range of Rows, Non-Contiguous Rows (like in a List-Box), a Single Cell, a Range of Cells or disable selection altogether.

This example allows you to pick the selection-mode from a Wijmo ComboBox control.

<c1-flex-grid id="smFlexGrid" is-read-only="true" auto-generate-columns="false" allow-sorting="true"> <c1-flex-grid-column binding="ID"></c1-flex-grid-column> <c1-flex-grid-column binding="Country" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount" format="n0" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Discount" format="p0" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Active" width="*"></c1-flex-grid-column> <c1-items-source source-collection="@Model.CountryData"></c1-items-source> </c1-flex-grid> <br />Selection Mode <c1-combo-box id="smMenu" selected-index="0" selected-index-changed="smMenu_SelectedIndexChanged"> <c1-items-source source-collection="@Model.Settings["SelectionMode"]"></c1-items-source> </c1-combo-box>
var smFlexGrid = smMenu = null; $(document).ready(function () { //Selection Modes Modules smFlexGrid = wijmo.Control.getControl("#smFlexGrid"); }); //Selection Modes Modules function smMenu_SelectedIndexChanged(sender){ if(sender.selectedValue!=null && smFlexGrid!=null){ smFlexGrid.selectionMode = sender.selectedValue; } }
using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using FlexGrid101.Models; using C1.Web.Mvc.Grid; namespace FlexGrid101.Controllers { public class HomeController : Controller { public ActionResult Index() { FlexGridModel model = new FlexGridModel(); model.Settings = CreateSettings(); model.CountryData = Sale.GetData(500); return View(model); } private IDictionary CreateSettings() { var settings = new Dictionary { {"SelectionMode",new object[]{SelectionMode.None.ToString(),SelectionMode.Cell.ToString(),SelectionMode.CellRange.ToString(),SelectionMode.Row.ToString(),SelectionMode.RowRange.ToString(),SelectionMode.ListBox.ToString()}} }; return settings; } } }

Result (live):


Selection Mode

Editing

FlexGrid has built-in support for fast, in-cell editing like you find in Excel. There is no need to add extra columns with Edit buttons that switch between display and edit modes.

Users can start editing by typing into any cell. This puts the cell in quick-edit mode. In this mode, pressing a cursor key finishes the editing and moves the selection to a different cell.

Another way to start editing is by pressing F2 or by clicking a cell twice. This puts the cell in full-edit mode. In this mode, pressing a cursor key moves the caret within the cell text. To finish editing and move to another cell, the user must press the Enter, Tab, or Escape key.

Data is automatically coerced to the proper type when editing finishes. If the user enters invalid data, the edit is cancelled and the original data remains in place.

You can disable editing at the Grid, Column, or Row levels using the is-read-only property of the Grid, Column, or Row objects. In this example, we make the ID and Country columns read-only.

<c1-flex-grid id="eFlexGrid" auto-generate-columns="false" allow-sorting="true"> <c1-flex-grid-column binding="ID" is-read-only="true"></c1-flex-grid-column> <c1-flex-grid-column binding="Country" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount" format="n0" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Discount" format="p0" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Active" width="*"></c1-flex-grid-column> <c1-items-source read-action-url="@Url.Action("GridRead")" update-action-url="@Url.Action("EGridUpdate")"></c1-items-source> </c1-flex-grid>
using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using FlexGrid101.Models; using C1.Web.Mvc.Grid; using C1.Web.Mvc; using C1.Web.Mvc.Serialization; namespace FlexGrid101.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GridRead([C1JsonRequest] CollectionViewRequest<Sale> requestData) { return this.C1Json(CollectionViewHelper.Read(requestData, FlexGridModel.Source)); } public ActionResult EGridUpdate([C1JsonRequest]CollectionViewEditRequest<Sale> requestData) { return this.C1Json(CollectionViewHelper.Edit(requestData, sale => { string error = string.Empty; bool success = true; var fSale = FlexGridModel.Source.Find(item => item.ID == sale.ID); fSale.Amount = sale.Amount; fSale.Discount = sale.Discount; fSale.Active = sale.Active; return new CollectionViewItemResult<Sale> { Error = error, Success = success, Data = fSale }; }, () => FlexGridModel.Source)); } } }

Result (live):

Grouping

FlexGrid supports grouping through the ICollectionView interface, which is identical to the one in .NET. To enable grouping, add one or more GroupDescription objects to the CollectionView.groupDescriptions property, and ensure that the grid's show-groups property is set to true (the default value).

GroupDescription objects are flexible, allowing you to group data based on value or on grouping functions. The example below groups dates by year; amounts by range returning three ranges: over 5,000, 1,000 to 5,000, 500 to 1,000, and under 500; and anything else by value. Use the menu to see the effects of each grouping.

Notice that the "Amount" column displays the totals in the group rows. We do this by setting the column's aggregate property to "C1.Web.Mvc.Grid.Aggregate.Sum". The aggregate is automatically updated when you edit the values in the column.

<c1-flex-grid id="gFlexGrid" auto-generate-columns="false" allow-sorting="true" group-by="Country"> <c1-flex-grid-column binding="Country" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Product" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Color" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Start" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Amount" format="n0" aggregate="Sum" width="*"></c1-flex-grid-column> <c1-flex-grid-column binding="Active" width="*"></c1-flex-grid-column> <c1-items-source source-collection="@Model.CountryData"></c1-items-source> </c1-flex-grid> <br />Group By <c1-combo-box id="gMenu" selected-index="0" selected-index-changed="gMenu_SelectedIndexChanged"> <c1-items-source source-collection="@Model.Settings["GroupBy"]"></c1-items-source> </c1-combo-box>
//Group By Modules function gMenu_SelectedIndexChanged(sender) { var grid = wijmo.Control.getControl("#gFlexGrid"); if (sender.selectedValue && grid) { var name = sender.selectedValue; var groupDescriptions = grid.collectionView.groupDescriptions; grid.beginUpdate(); groupDescriptions.clear(); if (name.indexOf("Country") > -1) { groupDescriptions.push(new wijmo.collections.PropertyGroupDescription("Country")); } if (name.indexOf("Product") > -1) { groupDescriptions.push(new wijmo.collections.PropertyGroupDescription("Product")); } if (name.indexOf("Color") > -1) { groupDescriptions.push(new wijmo.collections.PropertyGroupDescription("Color")); } if (name.indexOf("Start") > -1) { groupDescriptions.push(new wijmo.collections.PropertyGroupDescription("Start", function (item, prop) { var value = item[prop]; return value.getFullYear() + "/" + value.getMonth(); })); } if (name.indexOf("Amount") > -1) { groupDescriptions.push(new wijmo.collections.PropertyGroupDescription("Amount", function (item, prop) { var value = item[prop]; if (value <= 500) { return "<500"; } if (value > 500 && value <= 1000) { return "500 to 1000"; } if (value > 1000 && value <= 5000) { return "1000 to 5000"; } return "More than 5000"; })); } grid.endUpdate(); } }
using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; using FlexGrid101.Models; using C1.Web.Mvc.Grid; namespace FlexGrid101.Controllers { public class HomeController : Controller { public ActionResult Index() { FlexGridModel model = new FlexGridModel(); model.Settings = CreateSettings(); model.CountryData = Sale.GetData(500); return View(model); } private IDictionary CreateSettings() { var settings = new Dictionary { {"GroupBy", new object[]{"Country", "Product", "Color","Start","Amount","Country and Product","Product and Color", "None"}} }; return settings; } } }

Result (live):