NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

DataGridColumn Class

Specifies the appearance and text formatting and behavior of a DataGrid control column.

Object
   Component
      DataGridColumn

[Visual Basic]
MustInherit Public Class DataGridColumn
   Inherits Component
[C#]
public abstract class DataGridColumn : Component
[C++]
public __gc __abstract class DataGridColumn : public Component
[JScript]
public abstract class DataGridColumn extends Component

Remarks

The collection of DataGridColumn objects (the System.WinForms.DataGridColumnCollection) is accessed through the DataGrid control's GridColumns property.

The DataGrid control automatically creates a collection of DataGridColumn objects for you when the PopulateColumns method is invoked--the control creates one DataGridColumn for each DataColumn in the ColumnsCollection objects found in the DataTable. The objects created actually are instantiations of one of the following classes that either inherit directly or indirectly from DataGridColumn: DataGridBoolColumn, or DataGridTextBox, class.

When one of the derived classes is instantiated by a DataGrid control, the class created depends on the DataType of the DataColumn associated with the DataGridColumn object. For example, a DataColumn with its DataType set to System.Boolean will be associated with a DataGridColumn of type DataGridBoolColumn. To determine the type of any DataGridColumn, use the Object.GetType method.

To create your own column classes, you can inherit from DataGridColumn. You may want to do this in order to create special columns that host controls, as exemplified by the DataGridTextBox class, which hosts the TextBox control. For example, you may host an Image control to show pictures in columns, or you may create your own user control to host in the column.

The functionality of the DataGridColumn shouldn't be confused with that of the DataColumn. Whereas the DataColumn contains the properties and methods appropriate to creating a data table's schema, the DataGridColumn contains the properties and methods related to the appearance of an individual column on the screen.

To this end, the DataGridColumn contains appearance properties, such as the AlternatingBackColor, BackColor, ForeColor, Font, Header.

If a row contains a Value.DBNull, the text displayed in the column can be set with the NullText property.

The DataGridColumn class also allows you to specify the behavior of a column while its data is being changed. The BeginUpdate and EndUpdate methods temporarily suspend the drawing of the column while large updates are being made to the column's data. Without this functionality, every change in every cell of the grid would be immediately drawn; this could be distracting to the user and a performance liability.

Several methods allow monitoring of the column as it is edited by the user, including the Edit, Commit, and EnterNullValue events.

Most of the properties and methods of the class are tailored to controlling a column's appearance. But a few, such as the GetColumnValueAtRow and SetColumnValueAtRow allow you to examine and change the value in a specified cell. The CheckValidDataSource method similarly provides a method for checking the DataView.CheckValidDataSource for a DataGrid control.

Notes to Implementers: When you inherit from DataGridColumn, you must override the following members: Abort, Commit, Edit, and Paint (twice).

Requirements

Namespace: System.WinForms

Assembly: System.WinForms.dll

Example [Visual Basic]

The following example checks each DataGridColumn in a GridColumnsCollection and prints each object's Width, NullText, and ToString values.

[Visual Basic]

Public Sub TryForEach()
   Dim dgc As DataGridColumn
   Dim dgCols As GridColumnsCollection
   Set dgCols = datagrid1.GridColumns
   For Each dgc In dgCols
      Debug.Print dgc.Width, dgc.NullText, dgc.ToString
   Next
End Sub

See Also

DataGridColumn Members | System.WinForms Namespace | AlternatingBackColor | BackColor | CheckValidDataSource | DataColumn | DataGrid | DataGridBoolColumn | DataGridTextBox | DataGridTable | System.WinForms.DataView | DataSet | ForeColor | Font | Header | NullText