How to: Format the Windows Forms DataGrid Control Using the Designer
The DataGridView control replaces and adds functionality to the DataGrid control; however, the DataGrid control is retained for both backward compatibility and future use, if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.
Applying different colors to various parts of a DataGrid control can help to make the information in it easier to read and interpret. Color can be applied to rows and columns. Rows and columns can also be hidden or shown at your discretion.
There are three basic aspects of formatting the DataGrid control:
You can set properties to establish a default style in which data is displayed.
From that base, you can then customize the way certain tables are displayed at run time.
Finally, you can modify which columns are displayed in the data grid as well as the colors and other formatting that is shown.
As an initial step in formatting a data grid, you can set the properties of the DataGrid itself. These color and format choices form a base from which you can then make changes depending on the data tables and columns displayed.
The following procedure requires a Windows Application project with a form containing a DataGrid control. For information about setting up such a project, see How to: Create a New Windows Forms Application Project and How to: Add Controls to Windows Forms. In Visual Studio 2005, the DataGrid control is not in the Toolbox by default. For more information, see How to: Add Items to the Toolbox.
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
To establish a default style for the DataGrid control
Select the DataGrid control.
In the Properties window, set the following properties, as appropriate.
The BackColor property defines the color of the even-numbered rows of the grid. When you set the AlternatingBackColor property to a different color, every other row is set to this new color (rows 1, 3, 5, and so on).
The background color of the even-numbered rows of the grid (rows 0, 2, 4, 6, and so on).
Whereas the BackColor and AlternatingBackColor properties determines the color of rows in the grid, the BackgroundColor property determines the color of the area outside the row area, which is only visible when the grid is scrolled to the bottom, or if only a few rows are contained in the grid.
The grid's border style, one of the BorderStyle enumeration values.
The background color of the grid's window caption which appears immediately above the grid.
The font of the caption at the top of the grid.
The background color of the grid's window caption.
The font used to display the text in the grid.
The color of the font displayed by the data in the rows of the data grid.
The color of the grid lines of the data grid.
The style of the lines separating the cells of the grid, one of the DataGridLineStyle enumeration values.
The background color of row and column headers.
The font used for the column headers.
The foreground color of the grid's column headers, including the column header text and the plus sign (+) and minus sign (-) glyphs that expand and collapse rows when multiple related tables are displayed.
The color of text of all the links in the data grid, including links to child tables, the relation name, and so on.
In a child table, this is the background color of the parent rows.
In a child table, this is the foreground color of the parent rows.
Determines whether the table and column names are displayed in the parent row, by means of the DataGridParentRowsLabelStyle enumeration.
The default width (in pixels) of columns in the grid. Set this property before resetting the DataSource and DataMember properties (either separately, or through the SetDataBinding method), or the property will have no effect.
The property cannot be set to a value less than 0.
The row height (in pixels) of rows in the grid. Set this property before resetting the DataSource and DataMember properties (either separately, or through the SetDataBinding method), or the property will have no effect.
The property cannot be set to a value less than 0.
The width of the row headers of the grid.
When a row or cell is selected, this is the background color.
When a row or cell is selected, this is the foreground color.
When you are customizing the colors of controls, it is possible to make the control inaccessible due to poor color choice (for example, red and green). Use the colors available on the System Colors palette to avoid this issue.
The following procedure requires a DataGrid control bound to a data table. For more information, see How to: Bind the Windows Forms DataGrid Control to a Data Source.
To set the table and column style of a data table at design time
Select the DataGrid control on your form.
In the Properties window, select the TableStyles property and click the Ellipsis () button.
In the DataGridTableStyle Collection Editor dialog box, click Add to add a table style to the collection.
With the DataGridTableStyle Collection Editor, you can add and remove table styles, set display and layout properties, and set the mapping name for the table styles.
Set the MappingName property to the mapping name for each table style.
The mapping name is used to specify which table style should be used with which table.
In the DataGridTableStyle Collection Editor, select the GridColumnStyles property and click the ellipsis button ().
In the DataGridColumnStyle Collection Editor dialog box, add column styles to the table style you created.
With the DataGridColumnStyle Collection Editor, you can add and remove column styles, set display and layout properties, and set the mapping name and formatting strings for the data columns.
For more information about formatting strings, see Formatting Types.