Steps for getting started with the FlexGrid control in MVC applications are as follows:
Create a new MVC project using the C1 ASP.NET MVC application template, in Visual Studio IDE.
Add a controller and the corresponding view to the project.
Initialize the FlexGrid control in view using razor syntax.
(Optional) Add an appropriate CSS to customize the appearance of FlexGrid control.
This will create a FlexGrid with default behavior, which includes
automatic column generation, column sorting and reordering, editing,
and clipboard support.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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):
0
ID
Start
End
Country
Product
Color
Amount
Amount2
Discount
Active
Rank
1
1/25/2025
1/25/2025
German
Gadget
Green
581.61
-2,939.67
0.14
4
2
2/25/2025
2/25/2025
Canada
Gadget
Green
4,919.02
-4,673.75
0.17
2
3
3/25/2025
3/25/2025
Japan
Gadget
Red
2,159.73
-3,810.42
0.07
5
4
4/25/2025
4/25/2025
German
Gadget
Red
1,248.66
-2,815.93
0.22
1
5
5/25/2025
5/25/2025
German
Gadget
Black
4,051.76
-3,108.76
0.12
2
6
6/25/2025
6/25/2025
Canada
Gadget
Black
-3,131.28
-4,314.81
0.16
5
7
7/25/2025
7/25/2025
China
Widget
Red
698.62
-2,745.97
0.15
3
ID
Start
End
Country
Product
Color
Amount
Amount2
Discount
Active
Rank
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 Columns property.
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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):
0
ID
Start
End
Country
Amount
Amount2
Discount
Active
1
Jan 25 25
00:00
France
-1,227
3,217.05
3%
2
Feb 25 25
01:01
Japan
396
-284.69
13%
ID
Start
End
Country
Amount
Amount2
Discount
Active
Selection Modes
By default, FlexGrid allows you to select a range of cells with the mouse or keyboard,
just like Excel. The SelectionMode 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 SelectionMode from a C1 ASP.NET MVC's ComboBox control.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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<string, object[]> CreateSettings()
{
var settings = new Dictionary<string, object[]>
{
{"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):
0
ID
Country
Amount
Discount
Active
1
Canada
$1,722.93
3%
2
Canada
$4,521.50
10%
ID
Country
Amount
Discount
Active
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.
There are two modes for updating the data to the server.
By default, the update operation will be commit to the server once finishing editing.
Notices:
If the user wants to commit the update operation to the datasource server, the Update, Delete or Create action url should be provided.
And the corresponding codes used to update the datasource should be written in the corresponding action.
The other mode is called BatchEdit. The user can update, create or remove multiple items.
Once these modifications are confirmed, They could be commit to the data source only once.
Now these modifications can be commit by the commit method of CollectionView in client side.
The user can also commit them by sorting, paging or filtering behavior.
Notices:
In this mode, the BatchEdit action url should be provided and the corresponding codes used to update the datasource should be written.
You can disable editing at the Grid, Column, or Row levels using the IsReadOnly property of the
Grid, Column, or Row objects. In this example, we make the ID column read-only.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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<Sale>(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 && ModelState.IsValid,
Data = fSale
};
}, () => FlexGridModel.Source));
}
}
}
Result (live):
0
ID
Country
Amount
Discount
Active
ID
Country
Amount
Discount
Active
loading...
Grouping
FlexGrid supports grouping of client side data, set the GroupBy property to a column name for grouping the data according to a column.
Internally, FlexGrid supports grouping through the client side IItemsSource interface. To enable grouping at client side, add one or more GroupDescription objects to the
itemsSource.groupDescriptions property. It is easy to let the grid grouped by some field by calling the method GroupBy of FlexGridBuilder with the corresponding field name. And ensure that the grid's ShowGroups 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 "Sum." The aggregate is automatically
updated when you edit the values in the column.
//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;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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<string, object[]> CreateSettings()
{
var settings = new Dictionary<string, object[]>
{
{"GroupBy", new object[]{"Country", "Product", "Color","Start","Amount","Country and Product","Product and Color", "None"}}
};
return settings;
}
}
}
Result (live):
0
Country
Product
Color
Start
Amount
Country: France (48 items)
-5,809
France
Gadget
White
1/25/2025
3,934
Country
Product
Color
Start
Amount
Group By
Filtering
The FlexGrid supports filtering through the Filterable property. To enable filtering, set the Filterable(fl => fl.ColumnFilters() property.
In this example, we create a filter for the ID, Country, Product, Color, Start and get the filter value from the input control.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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);
model.FilterBy = new string[] { "ID", "Country", "Product", "Color", "Start" };
return View(model);
}
}
}
Result (live):
0
ID
Country
Product
Color
Start
Discount
1
UK
Gadget
Red
Jan 25 25
10%
2
France
Widget
Green
Feb 25 25
2%
ID
Country
Product
Color
Start
Discount
Paging
The FlexGrid supports paging through the Pager control. To enable paging, set the PageSize property to the number
of items you want on each page, and use Pager control to bind this FlexGrid.
In this example, we set PageSize to show 10 items per page. We add Pager control and set Owner Property to the FlexGrid id,
then we can switch pages.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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):
0
ID
Country
Product
Color
Start
Discount
1
Japan
Widget
Black
Jan 25 25
8%
2
Korea
Widget
Green
Feb 25 25
24%
3
Canada
Widget
Red
Mar 25 25
22%
4
Japan
Widget
Black
Apr 25 25
5%
5
China
Widget
Black
May 25 25
19%
6
Italy
Gadget
White
Jun 25 25
24%
7
US
Gadget
Red
Jul 25 25
5%
8
China
Gadget
Green
Aug 25 25
13%
9
Korea
Gadget
White
Sep 25 25
24%
10
Korea
Gadget
Red
Oct 25 25
13%
ID
Country
Product
Color
Start
Discount
Conditional Styling
FlexGrid has an ItemFormatter property that gives you complete control over
the contents of the cells.
This example uses a JavaScript function to create value ranges that return named
colors. We then call this function in the FlexGrid's ItemFormatter and pass the cell's data
in order to conditionally set the cell's foreground color.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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):
0
Country
Product
Discount
Amount
US
Gadget
14%
2,052
Italy
Gadget
6%
-4,418
Country
Product
Discount
Amount
Themes
The appearance of the FlexGrid is defined in CSS. In addition to the default theme, we
include about a dozen professionally designed themes that customize the appearance of
all C1 ASP.NET MVC controls to achieve a consistent, attractive look.
In this example, we add a "custom-flex-grid" class to the grid element and define some
CSS rules to create a simple "black and white, no borders" theme for any grids that
have the "custom-flex-grid" class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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):
0
Country
Product
Discount
Amount
Italy
Gadget
19%
-4,748
Japan
Gadget
3%
1,973
Country
Product
Discount
Amount
Trees and Hierarchical Data
In addition to grouping, FlexGrid supports hierarchical data, that is, data with items
that have lists of subitems. This type of hierarchical structure is very common, and is
usually displayed in a tree-view control.
To use FlexGrid with hierarchical data sources, set the ChildItemsPath property
to the name of the data element that contains the child elements. The grid automatically
scans the data and builds the tree for you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.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):
0
Folder/File Name
Size
.vs
config
Folder/File Name
Size
Handling null values
By default, FlexGrid allows you to enter empty values in columns of type string,
and will not allow empty/null values in columns of any other type.
You can change this behavior using the IsRequired property on grid columns.
If you set the IsRequired property to false, the grid will allow you to
enter empty values in that column, regardless of type. Conversely, if you set
the IsRequired property to true, the grid will not allow empty values
even in string columns.
Setting IsRequired to null reverts to the default behavior (nulls allowed
only in string columns).
The grid below reverts the default behavior. It sets IsRequired to false
for the first column, and to true for all others. You can delete content that
is not required by entering an empty string or simply by pressing the delete
key.