Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataGrid.TableStyles Property

Gets the collection of DataGridTableStyle objects for the grid.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[LocalizableAttribute(true)] 
public GridTableStylesCollection TableStyles { get; }
/** @property */
public GridTableStylesCollection get_TableStyles ()

public function get TableStyles () : GridTableStylesCollection

Not applicable.

Property Value

A GridTableStylesCollection that represents the collection of DataGridTableStyle objects.

Use the GridTableStylesCollection retrieved through the TableStyles property to create customized views of each table displayed by the System.Windows.Forms.DataGrid control.

By default, the collection returned by TableStyles property does not contain any DataGridTableStyle objects. To create a set of customized views:

  1. Create a DataGridTableStyle.

  2. Set the MappingName of the grid table object to the TableName of the DataTable.

  3. Add DataGridColumnStyle objects, one for each grid column you want to show, to the GridColumnStylesCollection returned by the GridColumnStyles property.

  4. Set the MappingName of each DataGridColumnStyle to the ColumnName of a DataColumn.

  5. Add the DataGridTableStyle object to the collection returned by TableStyles property.

Caution noteCaution:

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

The following code example creates one DataGridTableStyle for each DataTable found in a DataSet, and sets the MappingName of the DataGridTableStyle to the TableName of the DataTable. The DataGridTableStyle is then added to the GridTableStylesCollection returned by the TableStyles property. The example also prints the MappingName of each DataGridColumnStyle in the GridColumnStylesCollection returned by the GridColumnStyles property of each DataGridTableStyle in the GridTableStylesCollection.

private void AddTables(DataGrid myDataGrid, DataSet myDataSet){
   foreach(DataTable t in myDataSet.Tables){
      DataGridTableStyle myGridTableStyle = new 
      DataGridTableStyle();
      myGridTableStyle.MappingName = t.TableName;
      myDataGrid.TableStyles.Add(myGridTableStyle);

      /* Note that DataGridColumnStyle objects will
      be created automatically for the first DataGridTableStyle
      when you add it to the GridTableStylesCollection.*/
   }
}
private void PrintGridStyleInfo(DataGrid myDataGrid){
   /* Print the MappingName of each DataGridTableStyle,
   and the MappingName of each DataGridColumnStyle. */
   foreach(DataGridTableStyle myGridStyle in 
   myDataGrid.TableStyles){
   Console.WriteLine(myGridStyle.MappingName);
   foreach(DataGridColumnStyle myColumnStyle in 
      myGridStyle.GridColumnStyles){
     Console.WriteLine(myColumnStyle.MappingName);
      }
   }
}
 

private void AddTables(DataGrid myDataGrid, DataSet myDataSet)
{
    DataTable t;
    for (int iCtr = 0; iCtr < myDataSet.get_Tables().get_Count(); iCtr++) {
        t = myDataSet.get_Tables().get_Item(iCtr);
        DataGridTableStyle myGridTableStyle = new DataGridTableStyle();
        myGridTableStyle.set_MappingName(t.get_TableName());
        myDataGrid.get_TableStyles().Add(myGridTableStyle);
    }
} //AddTables

/*  Note that DataGridColumnStyle objects will
    be created automatically for the first DataGridTableStyle
    when you add it to the GridTableStylesCollection.
 */
private void PrintGridStyleInfo(DataGrid myDataGrid)
{
    DataGridTableStyle myGridStyle;
    for (int iCtr = 0; iCtr < myDataGrid.get_TableStyles().get_Count(); 
        iCtr++) {
        /*  Print the MappingName of each DataGridTableStyle,
            and the MappingName of each DataGridColumnStyle. 
         */
        myGridStyle = myDataGrid.get_TableStyles().get_Item(iCtr);
        Console.WriteLine(myGridStyle.get_MappingName());
        DataGridColumnStyle myColumnStyle;
        for (int iCtr1 = 0; 
            iCtr1 < myGridStyle.get_GridColumnStyles().get_Count();
            iCtr1++) {
            myColumnStyle = myGridStyle.get_GridColumnStyles().
                get_Item(iCtr1);
            Console.WriteLine(myColumnStyle.get_MappingName());
        }
    }
} //PrintGridStyleInfo

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show: