This documentation is archived and is not being maintained.

DataGridTableStyle Class

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

For a list of all members of this type, see DataGridTableStyle Members.


[Visual Basic]
Public Class DataGridTableStyle
   Inherits Component
public class DataGridTableStyle : Component
public __gc class DataGridTableStyle : public Component
public class DataGridTableStyle extends Component

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


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

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

  1. Populate the GridTableStylesCollection with DataGridTableStyle objects. If a DataGridTableStyle exists in the GridTableStylesCollection whose MappingName property value equals the DataTable object's TableName property, the DataTable is displayed with this DataGridTableStyle. If no DataGridTableStyle exists with a matching MappingName, the DataTable is displayed with the default style for data grid tables.
  2. When a different grid style is needed, use the Item property to select the appropriate DataGridTableStyle (pass the TableName to the Item property) and set the MappingName of the returned object to a new value.
  3. Use the Item property to select the desired DataGridTableStyle, and set its MappingName to the TableName of the DataTable.

To determine which DataGridTableStyle is currently displayed, use the DataSource and DataMember properties of the System.Windows.Forms.DataGrid to return a CurrencyManager. If the data source implements the ITypedList interface, you can use the GetListName method to return the 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 DataSet contains DataTable objects related through DataRelation objects, and the currently displayed DataTable is a child table, the DataMember will return a string in the form of TableName.RelationName (in the simplest case). If the DataTable is further down in the hierarchy, the string will consist of the parent table's name followed by the necessary RelationName values required to reach the table's level. For example, imagine three DataTable objects in a hierarchical relationship named (top to bottom) Regions, Customers, and Orders, and two DataRelation objects named RegionsToCustomers and CustomersToOrders, the DataMember property will return "Regions.RegionsToCustomers.CustomersToOrders". However, the MappingName will then be "Orders".

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

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

To bind the DataGrid to a strongly typed array of objects, the object must contain public properties. To create a DataGridTableStyle that displays such an array, set the MappingName property to classname[] where classname is replaced by the class name. Also note that the MappingName property is case-sensitive. 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. When binding to an ArrayList, set the MappingName of the DataGridTableStyle to "ArrayList" (the type name).


[Visual Basic, C#, C++] The following example sets creates two DataGridTableStyle instances and sets the MappingName of each object to the TableName of a DataTable in a DataSet. The example then adds DataGridColumnStyle objects to the GridColumnStylesCollection of each DataGridTableStyle. For an example that runs, see the System.Windows.Forms.DataGrid example.

[Visual Basic] 
Private Sub AddCustomDataTableStyle()
    ' Create a new DataGridTableStyle and set
    ' its MappingName to the TableName of a DataTable. 
    Dim ts1 As New DataGridTableStyle()
    ts1.MappingName = "Customers"
    ' Add a GridColumnStyle and set its MappingName
    ' to the name of a DataColumn in the DataTable.
    ' Set the HeaderText and Width properties. 
    Dim boolCol As New DataGridBoolColumn()
    boolCol.MappingName = "Current"
    boolCol.HeaderText = "IsCurrent Customer"
    boolCol.Width = 150
    ' Add a second column style.
    Dim TextCol As New DataGridTextBoxColumn()
    TextCol.MappingName = "custName"
    TextCol.HeaderText = "Customer Name"
    TextCol.Width = 250

    ' Create the second table style with columns.
    Dim ts2 As New DataGridTableStyle()
    ts2.MappingName = "Orders"

    ' Change the colors.
    ts2.ForeColor = Color.Yellow
    ts2.AlternatingBackColor = Color.Blue
    ts2.BackColor = Color.Blue
    ' Create new DataGridColumnStyle objects.
    Dim cOrderDate As New DataGridTextBoxColumn()
    cOrderDate.MappingName = "OrderDate"
    cOrderDate.HeaderText = "Order Date"
    cOrderDate.Width = 100
    Dim pcol As PropertyDescriptorCollection = Me.BindingContext(myDataSet, "Customers.custToOrders").GetItemProperties()
    Dim csOrderAmount As New DataGridTextBoxColumn(pcol("OrderAmount"), "c", True)
    csOrderAmount.MappingName = "OrderAmount"
    csOrderAmount.HeaderText = "Total"
    csOrderAmount.Width = 100

    ' Add the DataGridTableStyle objects to the collection.
End Sub 'AddCustomDataTableStyle

private void AddCustomDataTableStyle()
      /* Create a new DataGridTableStyle and set
      its MappingName to the TableName of a DataTable. */
      DataGridTableStyle ts1 = new DataGridTableStyle();
      ts1.MappingName = "Customers";

      /* Add a GridColumnStyle and set its MappingName 
      to the name of a DataColumn in the DataTable. 
      Set the HeaderText and Width properties. */
      DataGridColumnStyle boolCol = new DataGridBoolColumn();
      boolCol.MappingName = "Current";
      boolCol.HeaderText = "IsCurrent Customer";
      boolCol.Width = 150;
      // Add a second column style.
      DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
      TextCol.MappingName = "custName";
      TextCol.HeaderText = "Customer Name";
      TextCol.Width = 250;

      // Create the second table style with columns.
      DataGridTableStyle ts2 = new DataGridTableStyle();
      ts2.MappingName = "Orders";
      // Change the colors.
      ts2.ForeColor = Color.Yellow;
      ts2.AlternatingBackColor = Color.Blue;
      ts2.BackColor = Color.Blue;
      // Create new DataGridColumnStyle objects.
      DataGridColumnStyle cOrderDate = 
      new DataGridTextBoxColumn();
      cOrderDate.MappingName = "OrderDate";
      cOrderDate.HeaderText = "Order Date";
      cOrderDate.Width = 100;

      PropertyDescriptorCollection pcol = this.BindingContext
      [myDataSet, "Customers.custToOrders"].GetItemProperties();
      DataGridColumnStyle csOrderAmount = 
      new DataGridTextBoxColumn(pcol["OrderAmount"], "c", true);
      csOrderAmount.MappingName = "OrderAmount";
      csOrderAmount.HeaderText = "Total";
      csOrderAmount.Width = 100;

      // Add the DataGridTableStyle objects to the collection.

void AddCustomDataTableStyle()
      /* Create a new DataGridTableStyle and set
      its MappingName to the TableName of a DataTable. */
      DataGridTableStyle* ts1 = new DataGridTableStyle();
      ts1->MappingName = S"Customers";

      /* Add a GridColumnStyle and set its MappingName 
      to the name of a DataColumn in the DataTable. 
      Set the HeaderText and Width properties. */
      DataGridColumnStyle* boolCol = new DataGridBoolColumn();
      boolCol->MappingName = S"Current";
      boolCol->HeaderText = S"IsCurrent Customer";
      boolCol->Width = 150;
      // Add a second column style.
      DataGridColumnStyle* TextCol = new DataGridTextBoxColumn();
      TextCol->MappingName = S"custName";
      TextCol->HeaderText = S"Customer Name";
      TextCol->Width = 250;

      // Create the second table style with columns.
      DataGridTableStyle* ts2 = new DataGridTableStyle();
      ts2->MappingName = S"Orders";
      // Change the colors.
      ts2->ForeColor = Color::Yellow;
      ts2->AlternatingBackColor = Color::Blue;
      ts2->BackColor = Color::Blue;
      // Create new DataGridColumnStyle objects.
      DataGridColumnStyle* cOrderDate = 
      new DataGridTextBoxColumn();
      cOrderDate->MappingName = S"OrderDate";
      cOrderDate->HeaderText = S"Order Date";
      cOrderDate->Width = 100;

      PropertyDescriptorCollection* pcol = this->BindingContext
         ->Item[myDataSet, S"Customers.custToOrders"]->GetItemProperties();
      DataGridColumnStyle* csOrderAmount = 
         new DataGridTextBoxColumn(pcol->Item[S"OrderAmount"], S"c", true);
      csOrderAmount->MappingName = S"OrderAmount";
      csOrderAmount->HeaderText = S"Total";
      csOrderAmount->Width = 100;

      // Add the DataGridTableStyle objects to the collection.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

DataGridTableStyle Members | System.Windows.Forms Namespace | DataGrid | DataGridColumnStyle | GridColumnStylesCollection | GridTableStylesCollection