How to: Format the Windows Forms DataGrid Control

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.

To establish a default style for the DataGrid control

  1. 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 nonrow 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/minus glyphs (to expand 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

    Keep in mind, when customizing the colors of controls, that 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 procedures assume your form has a DataGrid control bound to a data table. For more information, see Binding the Windows Forms DataGrid Control to a Data Source.

To set the table and column style of a data table programmatically

  1. Create a new table style and set its properties.

  2. Create a column style and set its properties.

  3. Add the column style to the table style's column styles collection.

  4. Add the table style to the data grid's table styles collection.

  5. In the example below, create an instance of a new DataGridTableStyle and set its MappingName property.

  6. Create a new instance of a GridColumnStyle and set its MappingName (and some other layout and display properties).

  7. Repeat steps 2 through 6 for each column style you want to create.

    The following example illustrates how a DataGridTextBoxColumn is created, because a name is to be displayed in the column. Additionally, you add the column style to the GridColumnStylesCollection of the table style, and you add the table style to the GridTableStylesCollection of the data grid.

    Private Sub CreateAuthorFirstNameColumn()  
       ' Add a GridTableStyle and set the MappingName
       ' to the name of the DataTable.  
       Dim TSAuthors As New DataGridTableStyle()  
       TSAuthors.MappingName = "Authors"  
    
       ' Add a GridColumnStyle and set the MappingName
       ' to the name of a DataColumn in the DataTable.
       ' Set the HeaderText and Width properties.
       Dim TCFirstName As New DataGridTextBoxColumn()  
       TCFirstName.MappingName = "AV_FName"  
       TCFirstName.HeaderText = "First Name"  
       TCFirstName.Width = 75  
       TSAuthors.GridColumnStyles.Add(TCFirstName)  
    
       ' Add the DataGridTableStyle instance to
       ' the GridTableStylesCollection.
       myDataGrid.TableStyles.Add(TSAuthors)  
    End Sub  
    
    private void addCustomDataTableStyle()  
    {  
       // Add a GridTableStyle and set the MappingName
       // to the name of the DataTable.  
       DataGridTableStyle TSAuthors = new DataGridTableStyle();  
       TSAuthors.MappingName = "Authors";  
    
       // Add a GridColumnStyle and set the MappingName
       // to the name of a DataColumn in the DataTable.
       // Set the HeaderText and Width properties.
       DataGridColumnStyle TCFirstName = new DataGridTextBoxColumn();  
       TCFirstName.MappingName = " AV_FName";  
       TCFirstName.HeaderText = "First Name";  
       TCFirstName.Width = 75;  
       TSAuthors.GridColumnStyles.Add(TCFirstName);  
    
       // Add the DataGridTableStyle instance to
       // the GridTableStylesCollection.
       dataGrid1.TableStyles.Add(TSAuthors);  
    }  
    
    private:  
       void addCustomDataTableStyle()  
       {  
          // Add a GridTableStyle and set the MappingName
          // to the name of the DataTable.  
          DataGridTableStyle^ TSAuthors = new DataGridTableStyle();  
          TSAuthors->MappingName = "Authors";  
    
          // Add a GridColumnStyle and set the MappingName
          // to the name of a DataColumn in the DataTable.
          // Set the HeaderText and Width properties.
          DataGridColumnStyle^ TCFirstName = gcnew DataGridTextBoxColumn();  
          TCFirstName->MappingName = "AV_FName";  
          TCFirstName->HeaderText = "First Name";  
          TCFirstName->Width = 75;  
          TSAuthors->GridColumnStyles->Add(TCFirstName);  
    
          // Add the DataGridTableStyle instance to
          // the GridTableStylesCollection.
          dataGrid1->TableStyles->Add(TSAuthors);  
       }  
    

See also