Binding

In most scenarios, the FlexGrid shows data that is loaded from a server or generated in code. This data is represented as an array of objects, and the grid is bound to this array via its itemsSource property.

By default, when you set the itemsSource property the grid will scan the data objects and automatically generate columns for each property in the data items. For example:

Id
Country
Product
Active
Downloads
Sales
Expenses
Overdue
0
US
Phones
145,248
81,732.54
38,401.13
1
Germany
Computers
111,632
20,603.32
27,944.24
2
UK
Cameras
181,205
44,217.79
48,877.49
3
Japan
Stereos
54,740
29,190.63
23,365.74
4
Italy
Phones
126,531
46,951.19
49,107.56
5
Greece
Computers
6,073
86,237.02
49,767.35

Auto-generating columns works well for simple data sources or while testing/developing applications. But in most scenarios, you will want to turn off the automatic column generation and specify the columns in code. This way you get complete control over which columns are displayed, in what order, their widths, format, headers, etc.

Each column has a binding property that determines which property of the data item that should be displayed in the cell. For example:

Country
Downloads (k)
Sales
Expenses
Active
US
145
81,732.54
38,401.13
Germany
112
20,603.32
27,944.24
UK
181
44,217.79
48,877.49
Japan
55
29,190.63
23,365.74
Italy
127
46,951.19
49,107.56
Greece
6
86,237.02
49,767.35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// This file locates: "Scripts/Lesson/C1FlexGrid/Binding.js".
c1.documentReady(function () {
    var cv = c1.getService("collectionView");
    var data = cv.items;
 
    // auto-generated columns
    var theGridAutoGen = wijmo.Control.getControl('#theGridAutoGen');
    theGridAutoGen.itemsSource = data;
 
    // explicit columns
    var theGridExplicitCols = wijmo.Control.getControl('#theGridExplicitCols');
    theGridExplicitCols.initialize({
        autoGenerateColumns: false,
        columns: [
            { binding: 'Country', header: 'Country', width: '*' },
            { binding: 'Downloads', header: 'Downloads (k)', format: 'n0,' }, // thousands
            { binding: 'Sales', header: 'Sales', format: 'n2' },
            { binding: 'Expenses', header: 'Expenses', format: 'n2' },
            { binding: 'Active', header: 'Active' }
        ],
        itemsSource: data,
    });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Web.Mvc;
 
namespace LearnMvcClient.Controllers
{
    public partial class C1FlexGridController : Controller
    {
        // GET: Binding
        public ActionResult Binding()
        {
            return View(Models.FlexGridData.GetSales());
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@model IEnumerable<FlexGridData.Sale>
 
<h1>
    @Html.Raw(Resources.C1FlexGrid.Binding_Title)
</h1>
<p>
    @Html.Raw(Resources.C1FlexGrid.Binding_Text1)
</p>
 
@Html.C1().CollectionViewService().Id("collectionView").Bind(Model)
 
<p>
    @Html.Raw(Resources.C1FlexGrid.Binding_Text2)
</p>
@Html.C1().FlexGrid().Id("theGridAutoGen").ShowAlternatingRows(false).Height(200)
 
<p>
    @Html.Raw(Resources.C1FlexGrid.Binding_Text3)
</p>
<p>
    @Html.Raw(Resources.C1FlexGrid.Binding_Text4)
</p>
@Html.C1().FlexGrid().Id("theGridExplicitCols").ShowAlternatingRows(false).Height(200)