System.Windows.Forms.DataGridTableStyle Class

Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.

See Also: DataGridTableStyle Members


public class DataGridTableStyle : System.ComponentModel.Component, IDataGridEditingService


The System.Windows.Forms.DataGrid control displays data in the form of a grid. The System.Windows.Forms.DataGridTableStyle is a class that represents the drawn grid only. This grid should not be confused with the System.Data.DataTable class, which is a possible source of data for the grid. Instead, the System.Windows.Forms.DataGridTableStyle strictly represents the grid as it is painted in the control. Therefore, through the System.Windows.Forms.DataGridTableStyle you can control the appearance of the grid for each System.Data.DataTable. To specify which System.Windows.Forms.DataGridTableStyle is used when displaying data from a particular System.Data.DataTable, set the DataGridTableStyle.MappingName to the System.Data.DataTable.TableName of a System.Data.DataTable.

The System.Windows.Forms.GridTableStylesCollection retrieved through the DataGrid.TableStyles property contains all the System.Windows.Forms.DataGridTableStyle objects used by a System.Windows.Forms.DataGrid control. The collection can contain as many System.Windows.Forms.DataGridTableStyle objects as you need, however the DataGridTableStyle.MappingName of each must be unique. At run time, this allows you to substitute a different System.Windows.Forms.DataGridTableStyle for the same data, depending on the user's preference. To do this:

[The 'ordered' type of list has not been implemented in the ECMA stylesheet.]

Always create System.Windows.Forms.DataGridColumnStyle objects and add them to the System.Windows.Forms.GridColumnStylesCollection before adding System.Windows.Forms.DataGridTableStyle objects to the System.Windows.Forms.GridTableStylesCollection. When you add an empty System.Windows.Forms.DataGridTableStyle with a valid DataGridTableStyle.MappingName value to the collection, System.Windows.Forms.DataGridColumnStyle objects are automatically generated for you. Consequently, an exception will be thrown if you try to add new System.Windows.Forms.DataGridColumnStyle objects with duplicate DataGridTableStyle.MappingName values to the System.Windows.Forms.GridColumnStylesCollection.

To determine which System.Windows.Forms.DataGridTableStyle is currently displayed, use the DataGrid.DataSource and DataGrid.DataMember properties of the System.Windows.Forms.DataGrid to return a System.Windows.Forms.CurrencyManager. If the data source implements the System.ComponentModel.ITypedList interface, you can use the System.ComponentModel.ITypedList.GetListName(System.ComponentModel.PropertyDescriptor[]) method to return the DataGridTableStyle.MappingName of the current table. This is shown in the C# code below:


 private void PrintCurrentListName(DataGrid myDataGrid){
  CurrencyManager myCM = (CurrencyManager)
  BindingContext[myDataGrid.DataSource, myDataGrid.DataMember];
  IList myList = myCM.List;
  ITypedList thisList = (ITypedList) myList;

If the System.Data.DataSet contains System.Data.DataTable objects related through System.Data.DataRelation objects, and the currently displayed System.Data.DataTable is a child table, the DataGrid.DataMember will return a string in the form of TableName.RelationName (in the simplest case). If the System.Data.DataTable is further down in the hierarchy, the string will consist of the parent table's name followed by the necessary System.Data.DataRelation.RelationName values required to reach the table's level. For example, imagine three System.Data.DataTable objects in a hierarchical relationship named (top to bottom) Regions, Customers, and Orders, and two System.Data.DataRelation objects named RegionsToCustomers and CustomersToOrders, the DataGrid.DataMember property will return "Regions.RegionsToCustomers.CustomersToOrders". However, the DataGridTableStyle.MappingName will then be "Orders".

The collection of System.Windows.Forms.DataGridTableStyle objects is returned through the DataGrid.TableStyles property of the System.Windows.Forms.DataGrid.

When a System.Windows.Forms.DataGridTableStyle is displayed, the settings for the System.Windows.Forms.DataGridTableStyle will override the settings for the System.Windows.Forms.DataGrid control. If a value is not set for a particular System.Windows.Forms.DataGridTableStyle property, the System.Windows.Forms.DataGrid control's value will be used instead. The following list shows the System.Windows.Forms.DataGridColumnStyle properties that can be set to override System.Windows.Forms.DataGrid control properties:

To bind the System.Windows.Forms.DataGrid to a strongly typed array of objects, the object type must contain public properties. To create a System.Windows.Forms.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 DataGridTableStyle.MappingName property is case-sensitive; the type name must be matched exactly. See the DataGridTableStyle.MappingName property for an example.

You can also bind the System.Windows.Forms.DataGrid to an ArrayList. A feature of the ArrayList is that it can contain objects of multiple types, but the System.Windows.Forms.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 System.Windows.Forms.Control, the second item could be a System.Windows.Forms.TextBox (which inherits from System.Windows.Forms.Control). If, on the other hand, the first item is a System.Windows.Forms.TextBox, the second object cannot be a System.Windows.Forms.Control. Further, the ArrayList must have items in it when it is bound and the objects in the System.Windows.Forms.DataGridTableStyle must contain public properties. An empty ArrayList will result in an empty grid. When binding to an ArrayList, set the DataGridTableStyle.MappingName of the System.Windows.Forms.DataGridTableStyle to "ArrayList" (the type name).


Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0,