The System.Windows.Forms..::.DataGrid displays Web-like links to child tables. You can click on a link to navigate to the child table. When a child table is displayed, a back button appears in the caption that can be clicked to navigate back to the parent table. The data from the parent rows is displayed below the caption and above the column headers. You can hide the parent row information by clicking the button to the right of the back button.
To display a table in the System.Windows.Forms..::.DataGrid at run time, use the SetDataBinding method to set the DataSource and DataMember properties to a valid data source. The following data sources are valid:
For more information about the DataSet class, see DataSets, DataTables, and DataViews (ADO.NET).
You can create a grid that enables users to edit data but prevents them from adding new rows by using a DataView as the data source and setting the AllowNew property to false.
Data sources are further managed by BindingManagerBase objects. For each table in a data source, a BindingManagerBase can be returned from the form's BindingContext. For example, you can determine the number of rows contained by a data source by returning the associated BindingManagerBase object's Count property.
To validate data, use the underlying objects that represent data and their events. For example, if the data comes from a DataTable in a DataSet, use the ColumnChanging and RowChanging events.
To determine which cell is selected, use the CurrentCell property. Change the value of any cell by using the Item property, which can take either the row and column indexes of the cell, or a single DataGridCell. Monitor the CurrentCellChanged event to detect when the user selects another cell.
To determine which part of the control the user clicked, use the HitTest method in the MouseDown event. The HitTest method returns a DataGrid..::.HitTestInfo object, which contains the row and column of a clicked area.
To manage the appearance of the control at run time, several properties for setting the color and caption attributes are available, including the CaptionForeColor, CaptionBackColor, CaptionFont, and so on.
The appearance of the displayed grid (or grids) can be further modified by creating DataGridTableStyle objects and adding them to the GridTableStylesCollection, which is accessed through the TableStyles property. For example, if the DataSource is set to a DataSet containing three DataTable objects, you can add three DataGridTableStyle objects to the collection, one for each table. To synchronize each DataGridTableStyle object with a DataTable, set the MappingName of the DataGridTableStyle to the TableName of the DataTable. For more information about binding to an array of objects, see the DataGridTableStyle..::.MappingName property.
To create a customized view of a table, create an instance of a DataGridTextBoxColumn or DataGridBoolColumn class and add the object to the GridTableStylesCollection accessed through the TableStyles property. Both classes inherit from DataGridColumnStyle. For each column style, set the MappingName to the ColumnName of a column that you want to show in the grid. To hide a column, set its MappingName to something other than a valid ColumnName.
To format the text of a column, set the Format property of the DataGridTextBoxColumn to one of the values found in Date and Time Format Strings or Standard Numeric Format Strings.
To bind the DataGrid to a strongly typed array of objects, the object type must contain public properties. To create a DataGridTableStyle that displays the array, set the DataGridTableStyle..::.MappingName property to typename[] where typename is replaced by the name of the object type. Also note that the MappingName property is case-sensitive; the type name must be matched exactly. See the MappingName property for an example.
You can also bind the DataGrid to an ArrayList. A feature of the ArrayList is that it can contain objects of multiple types, but the DataGrid can only bind to such a list when all items in the list are of the same type as the first item. This means that all objects must either be of the same type, or they must inherit from the same class as the first item in the list. For example, if the first item in a list is a Control, the second item could be a TextBox (which inherits from Control). If, on the other hand, the first item is a TextBox, the second object cannot be a Control. Further, the ArrayList must have items in it when it is bound. An empty ArrayList will result in an empty grid. In addition, the objects in the ArrayList must contain public properties. When binding to an ArrayList, set the MappingName of the DataGridTableStyle to "ArrayList" (the type name).
For each DataGridTableStyle, you can set color and caption attributes that override the settings for the System.Windows.Forms..::.DataGrid control. However, if those properties are not set, the settings for the control are used by default. The following properties can be overridden by DataGridTableStyle properties:
To customize the appearance of individual columns, add DataGridColumnStyle objects to the GridColumnStylesCollection, which is accessed through the GridColumnStyles property of each DataGridTableStyle. To synchronize each DataGridColumnStyle with a DataColumn in the DataTable, set the MappingName to the ColumnName of a DataColumn. When constructing a DataGridColumnStyle, you can also set a formatting string that specifies how the column displays data. For example, you can specify that the column use a short-date format to display dates contained in the table.