How to: Format the Windows Forms DataGrid Control Using the Designer

Note

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 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.

To establish a default style for the DataGrid control

  1. Select the DataGrid control.

  2. In the Properties window, set the following properties, as appropriate.

    Property Description
    AlternatingBackColor 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).
    BackColor The background color of the even-numbered rows of the grid (rows 0, 2, 4, 6, and so on).
    BackgroundColor 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.
    BorderStyle The grid's border style, one of the BorderStyle enumeration values.
    CaptionBackColor The background color of the grid's window caption which appears immediately above the grid.
    CaptionFont The font of the caption at the top of the grid.
    CaptionForeColor The background color of the grid's window caption.
    Font The font used to display the text in the grid.
    ForeColor The color of the font displayed by the data in the rows of the data grid.
    GridLineColor The color of the grid lines of the data grid.
    GridLineStyle The style of the lines separating the cells of the grid, one of the DataGridLineStyle enumeration values.
    HeaderBackColor The background color of row and column headers.
    HeaderFont The font used for the column headers.
    HeaderForeColor 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.
    LinkColor The color of text of all the links in the data grid, including links to child tables, the relation name, and so on.
    ParentRowsBackColor In a child table, this is the background color of the parent rows.
    ParentRowsForeColor In a child table, this is the foreground color of the parent rows.
    ParentRowsLabelStyle Determines whether the table and column names are displayed in the parent row, by means of the DataGridParentRowsLabelStyle enumeration.
    PreferredColumnWidth 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.
    PreferredRowHeight 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.
    RowHeaderWidth The width of the row headers of the grid.
    SelectionBackColor When a row or cell is selected, this is the background color.
    SelectionForeColor When a row or cell is selected, this is the foreground color.

    Note

    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

  1. Select the DataGrid control on your form.

  2. In the Properties window, select the TableStyles property and click the Ellipsis (The Ellipsis button (...) in the Properties window of Visual Studio.) button.

  3. 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.

  4. 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.

  5. In the DataGridTableStyle Collection Editor, select the GridColumnStyles property and click the ellipsis button (The Ellipsis button (...) in the Properties window of Visual Studio.).

  6. 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.

    Note

    For more information about formatting strings, see Formatting Types.

See also