DataGridTableStyle Class
TOC
Collapse the table of content
Expand the table of content

DataGridTableStyle Class

 

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

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

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Windows.Forms.DataGridTableStyle

public class DataGridTableStyle : Component, IDataGridEditingService

NameDescription
System_CAPS_pubmethodDataGridTableStyle()

Initializes a new instance of the DataGridTableStyle class.

System_CAPS_pubmethodDataGridTableStyle(Boolean)

This API supports the product infrastructure and is not intended to be used directly from your code. Initializes a new instance of the DataGridTableStyle class using the specified value to determine whether the grid table is the default style.

System_CAPS_pubmethodDataGridTableStyle(CurrencyManager)

Initializes a new instance of the DataGridTableStyle class with the specified CurrencyManager.

NameDescription
System_CAPS_pubpropertyAllowSorting

Indicates whether sorting is allowed on the grid table when this DataGridTableStyle is used.

System_CAPS_pubpropertyAlternatingBackColor

Gets or sets the background color of odd-numbered rows of the grid.

System_CAPS_pubpropertyBackColor

Gets or sets the background color of even-numbered rows of the grid.

System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyColumnHeadersVisible

Gets or sets a value indicating whether column headers are visible.

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_pubpropertyDataGrid

Gets or sets the System.Windows.Forms.DataGrid control for the drawn table.

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyForeColor

Gets or sets the foreground color of the grid table.

System_CAPS_pubpropertyGridColumnStyles

Gets the collection of columns drawn for this table.

System_CAPS_pubpropertyGridLineColor

Gets or sets the color of grid lines.

System_CAPS_pubpropertyGridLineStyle

Gets or sets the style of grid lines.

System_CAPS_pubpropertyHeaderBackColor

Gets or sets the background color of headers.

System_CAPS_pubpropertyHeaderFont

Gets or sets the font used for header captions.

System_CAPS_pubpropertyHeaderForeColor

Gets or sets the foreground color of headers.

System_CAPS_pubpropertyLinkColor

Gets or sets the color of link text.

System_CAPS_pubpropertyLinkHoverColor

This API supports the product infrastructure and is not intended to be used directly from your code. Gets or sets the color displayed when hovering over link text.

System_CAPS_pubpropertyMappingName

Gets or sets the name used to map this table to a specific data source.

System_CAPS_pubpropertyPreferredColumnWidth

Gets or sets the width used to create columns when a new grid is displayed.

System_CAPS_pubpropertyPreferredRowHeight

Gets or sets the height used to create a row when a new grid is displayed.

System_CAPS_pubpropertyReadOnly

Gets or sets a value indicating whether columns can be edited.

System_CAPS_pubpropertyRowHeadersVisible

Gets or sets a value indicating whether row headers are visible.

System_CAPS_pubpropertyRowHeaderWidth

Gets or sets the width of row headers.

System_CAPS_pubpropertySelectionBackColor

Gets or sets the background color of selected cells.

System_CAPS_pubpropertySelectionForeColor

Gets or sets the foreground color of selected cells.

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

NameDescription
System_CAPS_pubmethodBeginEdit(DataGridColumnStyle, Int32)

Requests an edit operation.

System_CAPS_protmethodCreateGridColumn(PropertyDescriptor)

Creates a DataGridColumnStyle, using the specified property descriptor.

System_CAPS_protmethodCreateGridColumn(PropertyDescriptor, Boolean)

Creates a DataGridColumnStyle using the specified property descriptor. Specifies whether the DataGridColumnStyle is a default column style.

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Disposes of the resources (other than memory) used by the DataGridTableStyle.(Overrides Component.Dispose(Boolean).)

System_CAPS_pubmethodEndEdit(DataGridColumnStyle, Int32, Boolean)

Requests an end to an edit operation.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodOnAllowSortingChanged(EventArgs)

Raises the AllowSortingChanged event.

System_CAPS_protmethodOnAlternatingBackColorChanged(EventArgs)

Raises the AlternatingBackColorChanged event.

System_CAPS_protmethodOnBackColorChanged(EventArgs)

Raises the BackColorChanged event.

System_CAPS_protmethodOnColumnHeadersVisibleChanged(EventArgs)

Raises the ColumnHeadersVisibleChanged event.

System_CAPS_protmethodOnForeColorChanged(EventArgs)

Raises the ForeColorChanged event.

System_CAPS_protmethodOnGridLineColorChanged(EventArgs)

Raises the GridLineColorChanged event.

System_CAPS_protmethodOnGridLineStyleChanged(EventArgs)

Raises the GridLineStyleChanged event.

System_CAPS_protmethodOnHeaderBackColorChanged(EventArgs)

Raises the HeaderBackColorChanged event.

System_CAPS_protmethodOnHeaderFontChanged(EventArgs)

Raises the HeaderFontChanged event.

System_CAPS_protmethodOnHeaderForeColorChanged(EventArgs)

Raises the HeaderForeColorChanged event.

System_CAPS_protmethodOnLinkColorChanged(EventArgs)

Raises the LinkColorChanged event.

System_CAPS_protmethodOnLinkHoverColorChanged(EventArgs)

Raises the LinkHoverColorChanged event.

System_CAPS_protmethodOnMappingNameChanged(EventArgs)

Raises the MappingNameChanged event

System_CAPS_protmethodOnPreferredColumnWidthChanged(EventArgs)

Raises the PreferredColumnWidthChanged event.

System_CAPS_protmethodOnPreferredRowHeightChanged(EventArgs)

Raises the PreferredRowHeightChanged event.

System_CAPS_protmethodOnReadOnlyChanged(EventArgs)

Raises the ReadOnlyChanged event.

System_CAPS_protmethodOnRowHeadersVisibleChanged(EventArgs)

Raises the RowHeadersVisibleChanged event.

System_CAPS_protmethodOnRowHeaderWidthChanged(EventArgs)

Raises the RowHeaderWidthChanged event.

System_CAPS_protmethodOnSelectionBackColorChanged(EventArgs)

Raises the SelectionBackColorChanged event.

System_CAPS_protmethodOnSelectionForeColorChanged(EventArgs)

Raises the SelectionForeColorChanged event.

System_CAPS_pubmethodResetAlternatingBackColor()

Resets the AlternatingBackColor property to its default value.

System_CAPS_pubmethodResetBackColor()

Resets the BackColor property to its default value.

System_CAPS_pubmethodResetForeColor()

Resets the ForeColor property to its default value.

System_CAPS_pubmethodResetGridLineColor()

Resets the GridLineColor property to its default value.

System_CAPS_pubmethodResetHeaderBackColor()

Resets the HeaderBackColor property to its default value.

System_CAPS_pubmethodResetHeaderFont()

Resets the HeaderFont property to its default value.

System_CAPS_pubmethodResetHeaderForeColor()

Resets the HeaderForeColor property to its default value.

System_CAPS_pubmethodResetLinkColor()

Resets the LinkColor property to its default value.

System_CAPS_pubmethodResetLinkHoverColor()

This API supports the product infrastructure and is not intended to be used directly from your code. Resets the LinkHoverColor property to its default value.

System_CAPS_pubmethodResetSelectionBackColor()

Resets the SelectionBackColor property to its default value.

System_CAPS_pubmethodResetSelectionForeColor()

Resets the SelectionForeColor property to its default value.

System_CAPS_protmethodShouldSerializeAlternatingBackColor()

Indicates whether the AlternatingBackColor property should be persisted.

System_CAPS_protmethodShouldSerializeBackColor()

Indicates whether the BackColor property should be persisted.

System_CAPS_protmethodShouldSerializeForeColor()

Indicates whether the ForeColor property should be persisted.

System_CAPS_protmethodShouldSerializeGridLineColor()

Indicates whether the GridLineColor property should be persisted.

System_CAPS_protmethodShouldSerializeHeaderBackColor()

Indicates whether the HeaderBackColor property should be persisted.

System_CAPS_protmethodShouldSerializeHeaderForeColor()

Indicates whether the HeaderForeColor property should be persisted.

System_CAPS_protmethodShouldSerializeLinkColor()

Indicates whether the LinkColor property should be persisted.

System_CAPS_protmethodShouldSerializeLinkHoverColor()

This API supports the product infrastructure and is not intended to be used directly from your code. Indicates whether the LinkHoverColor property should be persisted.

System_CAPS_protmethodShouldSerializePreferredRowHeight()

Indicates whether the PreferredRowHeight property should be persisted.

System_CAPS_protmethodShouldSerializeSelectionBackColor()

Indicates whether the SelectionBackColor property should be persisted.

System_CAPS_protmethodShouldSerializeSelectionForeColor()

Indicates whether the SelectionForeColor property should be persisted.

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticDefaultTableStyle

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the default table style.

NameDescription
System_CAPS_pubeventAllowSortingChanged

Occurs when the AllowSorting property value changes.

System_CAPS_pubeventAlternatingBackColorChanged

Occurs when the AlternatingBackColor value changes.

System_CAPS_pubeventBackColorChanged

Occurs when the BackColor value changes.

System_CAPS_pubeventColumnHeadersVisibleChanged

Occurs when the ColumnHeadersVisible value changes.

System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

System_CAPS_pubeventForeColorChanged

Occurs when the ForeColor value changes.

System_CAPS_pubeventGridLineColorChanged

Occurs when the GridLineColor value changes.

System_CAPS_pubeventGridLineStyleChanged

Occurs when the GridLineStyle value changes.

System_CAPS_pubeventHeaderBackColorChanged

Occurs when the HeaderBackColor value changes.

System_CAPS_pubeventHeaderFontChanged

Occurs when the HeaderFont value changes.

System_CAPS_pubeventHeaderForeColorChanged

Occurs when the HeaderForeColor value changes.

System_CAPS_pubeventLinkColorChanged

Occurs when the LinkColor value changes.

System_CAPS_pubeventLinkHoverColorChanged

This API supports the product infrastructure and is not intended to be used directly from your code. Occurs when the LinkHoverColor value changes.

System_CAPS_pubeventMappingNameChanged

Occurs when the MappingName value changes.

System_CAPS_pubeventPreferredColumnWidthChanged

Occurs when the PreferredColumnWidth property value changes.

System_CAPS_pubeventPreferredRowHeightChanged

Occurs when the PreferredRowHeight value changes.

System_CAPS_pubeventReadOnlyChanged

Occurs when the ReadOnly value changes.

System_CAPS_pubeventRowHeadersVisibleChanged

Occurs when the RowHeadersVisible value changes.

System_CAPS_pubeventRowHeaderWidthChanged

Occurs when the RowHeaderWidth value changes.

System_CAPS_pubeventSelectionBackColorChanged

Occurs when the SelectionBackColor value changes.

System_CAPS_pubeventSelectionForeColorChanged

Occurs when the SelectionForeColor value changes.

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 retrieved through the TableStyles property 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.

System_CAPS_cautionCaution

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.

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;
 Console.WriteLine(thisList.GetListName(null));
}

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 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 and the objects in the DataGridTableStyle must contain public properties. 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).

The following code example 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.

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;
      ts1.GridColumnStyles.Add(boolCol);

      // Add a second column style.
      DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
      TextCol.MappingName = "custName";
      TextCol.HeaderText = "Customer Name";
      TextCol.Width = 250;
      ts1.GridColumnStyles.Add(TextCol);


      // 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;
      ts2.GridColumnStyles.Add(cOrderDate);

      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;
      ts2.GridColumnStyles.Add(csOrderAmount);

      // Add the DataGridTableStyle objects to the collection.
      myDataGrid.TableStyles.Add(ts1);
      myDataGrid.TableStyles.Add(ts2);
   }

.NET Framework
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft