Arrays and CollectionViews

When you set the grid's itemsSource property to a regular JavaScript array, it automatically creates an internal CollectionView and uses that as a data source so that it can provide sorting and editing features without forcing you to create a CollectionView yourself.

This internal view is exposed by the grid's collectionView property, and you can use it in case you need the extra functionality yourself.

For example, the grid below is bound to a regular array, and the grid's collectionView property is used to show the currently selected item:

Country: US, Sales: $5,570 Expenses: $3,606
Country
Sales
Expenses
Germany
5,934.09
2,078.45
Greece
8,654.23
4,760.74
Italy
4,510.24
1,647.68
Japan
3,837.87
332.94
UK
2,997.96
397.78
US
5,570.39
3,606.01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// This file locates: "Scripts/Lesson/C1FlexGrid/CollectionView.js".
c1.documentReady(function () {
    // create some random data
    var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
    var data = [];
    for (var i = 0; i < countries.length; i++) {
        data.push({
            id: i,
            country: countries[i],
            sales: Math.random() * 10000,
            expenses: Math.random() * 5000
        });
    }
 
    var theGrid = wijmo.Control.getControl('#theGrid');
    theGrid.itemsSource = data;
 
    // show the current item
    var selItemElement = document.getElementById('selectedItem');
    function updateCurrentInfo() {
        selItemElement.innerHTML = wijmo.format(
            'Country: <b>{country}</b>, Sales: <b>{sales:c0}</b> Expenses: <b>{expenses:c0}</b>',
          theGrid.collectionView.currentItem);
    }
 
    // update current item now and when the grid selection changes
    updateCurrentInfo();
    theGrid.collectionView.currentChanged.addHandler(updateCurrentInfo);
 
    // sort the data by country
    var sd = new wijmo.collections.SortDescription('country', true);
    theGrid.collectionView.sortDescriptions.push(sd);
});
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: CollectionView
        public ActionResult CollectionView()
        {
            return View();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<h1>
    @Html.Raw(Resources.C1FlexGrid.CollectionView_Title)
</h1>
<p>
    @Html.Raw(Resources.C1FlexGrid.CollectionView_Text1)
</p>
<p>
    @Html.Raw(Resources.C1FlexGrid.CollectionView_Text2)
</p>
<p>
    @Html.Raw(Resources.C1FlexGrid.CollectionView_Text3)
</p>
<div id="selectedItem"></div>
@(Html.C1().FlexGrid().Id("theGrid")
    .AllowSorting(false)
    .ShowSort(false)
    .AutoGenerateColumns(false)
    .Columns(cs=>
    {
        cs.Add().Binding("country").Header("Country");
        cs.Add().Binding("sales").Header("Sales").Format("n2");
        cs.Add().Binding("expenses").Header("Expenses").Format("n2");
    })
)