Nevron Open Vision Documentation
Data Binding and Column Generation

 
Binding the Grid To Data

To bind the grid to data you need to set its DataSource property like in the following example:

Binding the Grid to a Data Source
Copy Code
// create a data table with two fields
NMemoryDataTable dataTable = new NMemoryDataTable(
    new NFieldInfo("Name", typeof(string)),
    new NFieldInfo("Birthday", typeof(DateTime)));
           
// fill the data table
dataTable.AddRow("George", new DateTime(1976, 12, 19));
dataTable.AddRow("Andrew", new DateTime(1982, 9, 27));
// create a view and get its grid
NTableGridView view = new NTableGridView();
NTableGrid grid = view.Grid;
// bind the grid to data
grid.DataSource = new NDataSource(dataTable);

As discussed in the Data Sources topic, a data source is an attribute that aggregates a data table. Data tables help you define data in-memory (NMemoryDataTable) or bind to different collections of objects as summarized by the following table:

Data Tables for binding to Object Collections
Binding Interface Data Table Type
IList NIListDataTable
IEnumerable NIEnumerableDataTable
IList<> NGenericIListDataTable
IEnumerable<> NGenericIEnumerableDataTable
INCollection<> NGenericINCollectionDataTable
INIterable<> NGenericINIterableDataTable

Additionally specific NOV hosts can even extend the types of objects that you represent as NDataTable with platform specific adaptations. For example hosts where the System.Data.DataTable is defined, can create a certain type of NDataTable that can wrap around a System.Data.DataTable. See the Data Tables topic for more info.

Automatic Column Creation

When you set the DataSource property, the grid automatically creates a column for each field of the data source, if the AutoCreateColumns property is set to true. During the automatic column creation, the grid raises the AutoCreateColumn event, that lets you modify the automatically created column for a certain data source field, or prevent the creation of columns for certain fields. For example:

Handling AutoCreateColumn event
Copy Code
// bind the grid to the data source, but exclude the "PersonId" column.
grid.AutoCreateColumn += delegate(NAutoCreateColumnEventArgs arg)
{
    switch (arg.FieldInfo.Name)
    {
        case "Name":
            // do not create a column for the Name field.
            arg.DataColumn = null;
            break;
        case "Birthday":
            // change the Birthday column cells background
            arg.DataColumn.Format.BackgroundFill = new NColorFill(NColor.LightBlue);
            break;
        default:
            break;
    }
};
// bind the grid to data
grid.AutoCreateColumns = true;
grid.DataSource = new NDataSource(dataTable);

Note that the automatic column generation produces data columns (instances of the NDataColumn class).

Manual Column Creation

Sometimes you may want to explicitly create the columns of the grid. In this case you can turn off the automatic column creation and manually add the columns you want, like this:

Manual Column Creation
Copy Code
// turn off automatic column generation and bind to the data source
grid.AutoCreateColumns = false;
grid.DataSource = new NDataSource(dataTable);
// create a data column that is bound to the Birthday field
NDataColumn birthdayColumn = new NDataColumn();
birthdayColumn.Bind(grid.DataSource, "Birthday");
grid.Columns.Add(birthdayColumn);
// create a data column that is bound to the Name field
NDataColumn nameColumn = new NDataColumn();
nameColumn.Bind(grid.DataSource, "Name");
grid.Columns.Add(nameColumn);

As demonstrated by the example the binding of a data column to a specific data source field is achieved by the Bind method. 

Send Feedback