Specifies the appearance, text formatting, and behavior of a System.Windows.Forms.DataGrid control column. This class is abstract.
See Also: DataGridColumnStyle Members
The collection of System.Windows.Forms.DataGridColumnStyle objects (the System.Windows.Forms.GridColumnStylesCollection) is accessed through the System.Windows.Forms.DataGrid control's DataGrid.TableStyles property.
The System.Windows.Forms.DataGrid control automatically creates a collection of System.Windows.Forms.DataGridColumnStyle objects for you when you set the DataGrid.DataSource property to an appropriate data source. The objects created actually are instances of one of the following classes that inherit from System.Windows.Forms.DataGridColumnStyle: System.Windows.Forms.DataGridBoolColumn or System.Windows.Forms.DataGridTextBoxColumn class.
To format the data display, set the DataGridTextBoxColumn.Format property of the System.Windows.Forms.DataGridTextBoxColumn class to one of the formatting values. For more information about valid formatting values, see Formatting Types and Custom DateTime Format Strings.
You can also create your own set of System.Windows.Forms.DataGridColumnStyle objects and add them to the System.Windows.Forms.GridColumnStylesCollection. When you do so, you must set the DataGridColumnStyle.MappingName of each column style to the System.Data.DataColumn.ColumnName of a System.Data.DataColumn to synchronize the display of columns with the actual data.
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.
When one of the derived classes is instantiated by a System.Windows.Forms.DataGrid control, the class created depends on the System.Data.DataColumn.DataType of the System.Data.DataColumn associated with the System.Windows.Forms.DataGridColumnStyle object. For example, a System.Data.DataColumn with its System.Data.DataColumn.DataType set to System.Boolean will be associated with a System.Windows.Forms.DataGridBoolColumn. To determine the type of any System.Windows.Forms.DataGridColumnStyle, use the object.GetType method.
To create your own column classes, you can inherit from System.Windows.Forms.DataGridColumnStyle. You might want to do this in order to create special columns that host controls, as exemplified by the System.Windows.Forms.DataGridTextBox class, which hosts the System.Windows.Forms.TextBox control. For example, you can host an System.Drawing.Image control to show pictures in columns, or you can create your own user control to host in the column.
The functionality of the System.Windows.Forms.DataGridColumnStyle shouldn't be confused with that of the System.Data.DataColumn. Whereas the System.Data.DataColumn contains the properties and methods appropriate to creating a data table's schema, the System.Windows.Forms.DataGridColumnStyle contains the properties and methods related to the appearance of an individual column on the screen.
The System.Windows.Forms.DataGridColumnStyle class also allows you to specify the behavior of a column while its data is being changed. The DataGridColumnStyle.BeginUpdate and DataGridColumnStyle.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 DataGridColumnStyle.Edit(CurrencyManager, int, System.Drawing.Rectangle, bool) and DataGridColumnStyle.Commit(CurrencyManager, int) events.
Most of the properties and methods of the class are tailored to controlling a column's appearance. But a few, such as the DataGridColumnStyle.GetColumnValueAtRow(CurrencyManager, int) and DataGridColumnStyle.SetColumnValueAtRow(CurrencyManager, int, object) allow you to examine and change the value in a specified cell.