The grid can be populated with data automatically by binding it to a data source, or data can be provided manually (unbound). It is also possible to use combinations of both. The data contained within the grid can also be displayed in a hierarchical master/detail view.
Regardless of if the grid has been populated automatically or manually, its data can be edited programmatically or using cell editors.
In order to bind the grid to a data source (populate the grid automatically), the DataSource and optionally the DataMember properties must be set or the SetDataBinding method called. If another control is bound to the same data source as the grid, then both the DataSource and DataMember properties must be set in order for the other control to be synchronized with the grid.
The grid supports a variety of data sources which can be used to automatically populate the grid with data. Theses data sources are: DataTables, DataViews, DataSets, DataViewManagers, any component that implements the IList or IListSource interface and jagged arrays. For more information on how to bind the grid to a data source and the various supported data sources, jump to the Data Binding topic.
Data can also be provided manually to the grid by adding new DataRow objects to the grid's collection of DataRows using the DataRows' collection AddNew method. The AddNew method will return a new instance of a DataRow object for which data can be provided, or the AddingDataRow event can be used to provide the row with its data. In either case, the result will be the same.
If the AddNew method has been called to add a new DataRow in the grid, the EndEdit method must be called afterwards in order to actually add the newly created DataRow to the grid. If EndEdit is not called and another call to AddNew is made, the previous row created with AddNew will be added to the grid. For more information on how to provided data to the grid manually, jump to the Providing data manually topic.
The grid also supports populating the grid with both bound data (data automatically retrieved from a data source) and unbound data (data provided manually) by allowing you add custom (unbound) columns in addition to the automatically created DataBoundColumns. For more information on how to combine bound and unbound data, jump to the Combining bound and unbound data topic.
At run-time, it is also possible to provide data to the grid using the InsertionRow which allows you to add new DataRows to the grid. The InsertionRow row can be added to any of the grid's sections (as explained in the overview of the grid elements).
In addition to displaying data in a standard "flat" view, Xceed Grid for WinForms can also display data in a hierarchical master/detail view. Like the main grid, detail grids can be populated with bound or unbound data or with combinations of both.
Each DataRow in the main grid as well as each DataRow in any detail grid can have an unlimited number of detail grids "attached" to it. The number of DetailGrid objects added to the parent grid's DetailGridTemplates collection will determine the number of detail grid's attached to the DataRows of that particular grid. For example, if you add 2 DetailGrid objects to the main grid's DetailGridTemplates property, each DataRow in the main grid will have 2 detail grids associated with it.
To bind the detail grid to the detail data, set the DataSource property to null (Nothing in Visual Basic) and the DataMember property to the name of the relationship in the dataset. It is also possible to set the DataSource property of the detail grid to the same data source as the main grid and the DataMember property of the detail grid to the name of the table (in the case of a dataset) and the field created by the relationship.
Like the main grid, data can be provided manually to the detail grids by adding new DataRow objects to the detail grid's collection of DataRows using the DataRows' collection AddNew method. The AddNew method will return a new instance of a DataRow object for which data can be provided, or the AddingDataRow event can be used to provide the row with its data. In either case, the result will be the same. The InitializingDetailGrid event can also be used to populate/modify the contents of a detail grid depending on the DataRow to which it is associated.
For more information on creating a master/detail grid, jump to the Tutorials section.
Once the grid has been populated with data (either automatically or manually), its data can be edited using CellEditorManagers. Depending on the data type of the cell's data, in some cases, a CellEditorManager will be automatically assigned to the cell or column. For example, if a cell or column contains a DateTime value, the DateEditor will be used when editing the data. Each cell or column always has a CellEditorManager.
The grid also allows you to change the CellEditorManager for a specific cell or column by setting the cell's or column's CellEditorManager property. You can either assign one of the predefined CellEditorManagers, use the CellEditorManager class to "wrap" a control, or create a class that derives from the CellEditorManager class. For more information on how to change the cell editor for a specific cell or column, jump to the CellEditorManagers topic.
It is also possible to force a cell to enter edit mode using the EnterEdit method. The modified data can then be committed (or not) using the LeaveEdit method.
Using the EnterEdit method will invoke the cell's editor and raise the EnteringEdit event. In the EnteringEdit event, it is possible to change the CellEditorManager that will be used to edit the content of the cell or prevent the user from entering into edit mode.
To commit the changes made to a cell's content, the LeaveEdit method must be called and its commit parameter set to true. To discard the changes, the same method must be called, but with its parameter set to false. Calling the LeaveEdit method with commit set to true will also raise the LeavingEdit event in which you can also cancel the modifications made to the cell's content or change the value that is about to be committed.
By default, if a value is provided to a cell and it does not pass the validation process, the result is the equivalent of calling the LeaveEdit method with its commit parameter set to false: the new value will be discarded and the cell will leave edit mode. No exception will be thrown by the grid if a value fails validation. For more information on how to edit a cell's data programmatically, jump to the Editing cells topic.
Keep in mind that the data source is not automatically updated when editing the grid. For example, if the data source is a dataset, only the in-memory data will be updated. It is up to you to update the physical data source.
Of course, it is also possible to prevent the edition of the grid's data by setting the ReadOnly property to true. It is also possible to prevent the edition of a specific cell(s) by setting its ReadOnly property to true.
The data in the grid can be displayed using various CellViewerManagers. In some cases, depending on the data type of the data, a CellViewerManager will automatically be assigned to the cell or column. For example, if a cell or column contains boolean values, the data will be displayed using the CheckBoxViewer. This means that rather than displaying "true" or "false", a checked (or unchecked) check box will be displayed. In cases were a CellViewerManager is not assigned to cell or column, the ToString representation of the cell's data will be displayed.
The grid allows you to change the CellViewerManager for a specific cell or for an entire column by setting the cell's or the column's CellViewerManager property. You can either assign one of the predefined CellViewerManagers, use the CellViewerManager class to "wrap" a control, or create a class that derives from the CellViewerManager class. For more information on how to change the cell viewer for a specific cell or column, jump to the CellViewerManagers topic.