Export (0) Print
Expand All

Formatting the Windows Forms DataGrid Control

Visual Studio .NET 2003

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.

Setting Format Properties at Design Time

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

  • Set the following properties as appropriate:
    PropertyDescription
    AlternatingBackColorThe 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).
    BackColorThe background color of the even-numbered rows of the grid (rows 0, 2, 4, 6, and so on).
    BackgroundColorWhereas 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.
    BorderStyleThe grid's border style, one of the BorderStyle enumeration values.
    CaptionBackColorThe background color of the grid's window caption which appears immediately above the grid.
    CaptionFontThe font of the caption at the top of the grid.
    CaptionForeColorThe background color of the grid's window caption.
    FontThe font used to display the text in the grid.
    ForeColorThe color of the font displayed by the data in the rows of the data grid.
    GridLineColorThe color of the grid lines of the data grid.
    GridLineStyleThe style of the lines separating the cells of the grid, one of the DataGridLineStyle enumeration values.
    HeaderBackColorThe background color of row and column headers.
    HeaderFontThe font used for the column headers.
    HeaderForeColorThe 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).
    LinkColorThe color of text of all the links in the data grid, including links to child tables, the relation name, and so on.
    ParentRowsBackColorIn a child table, this is the background color of the parent rows.
    ParentRowsForeColorIn a child table, this is the foreground color of the parent rows.
    ParentRowsLabelStyleDetermines whether the table and column names are displayed in the parent row, by means of the DataGridParentRowsLabelStyle enumeration.
    PreferredColumnWidthThe 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.

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

    RowHeaderWidthThe width of the row headers of the grid.
    SelectionBackColorWhen a row or cell is selected, this is the background color.
    SelectionForeColorWhen 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.

Table Styles and Column Styles

Once you have established the default format of the DataGrid control, you can customize the colors that will be used when certain tables are displayed within the data grid.

This is achieved through creating instances of the DataGridTableStyles class. Table styles specify the formatting of specific tables, distinct from the default formatting of the DataGrid control itself. Each table may have only one table style defined for it at a time.

Sometimes, you will want to have a specific column look different than the rest of the columns of a given data table. You can achieve this by means of the GridColumnStyles class, which allows you to create a customized set of column styles.

Column styles are related to columns in a dataset in the same way that table styles are related to data tables. Just as each table may only have one table style defined for it at a time, so too can each column only have one column style defined for it, in a given table style — this relationship is defined in the column's MappingName property.

Be aware that if you have created a table style with no column styles added to it, Visual Studio will add default column styles when the form and grid are created at run time. However, if you have created a table style and added any column styles to it, Visual Studio will not create any column styles (and you will need to define column styles and assign them with the mapping name to have the desired columns appear in the grid.)

Since you specify which columns are included in the data grid (by assigning them a column style), it is possible to include columns of data in the dataset that are not displayed in the grid because no column style has been assigned to them. However, keep in mind that, because the data column is included in the dataset, it is possible for a user to programmatically edit the data that is not displayed.

Note   In general, create column styles and add them to the column styles collection before adding table styles to the table styles collection. When you add an empty table style to the collection, column styles are automatically generated for you. Consequently, an exception will be thrown if you try to add new column styles with duplicate MappingName values to the column styles collection.
In some cases, you will want to just tweak one column among a large number of columns (for example, the dataset contains 50 columns and you only want 49 of them). In this case, it is easier to import all 50 columns and programmatically remove one, rather than programmatically adding each of the 49 individual columns you want.

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 at design time

  1. Select the data grid control on your form.
  2. In the Properties window, select the TableStyles property and click the ellipsis button (Aa984371.vbellipsesbutton(en-us,VS.71).gif).

    The DataGridTableStyle Collection Editor opens. In this dialog box you can add and remove table styles, set display and layout properties, and set the mapping name for the table styles.

  3. Add a table style to the collection by clicking the Add button.
  4. Set the mapping name for the table styles in the MappingName property. The mapping name is used to specify which table style should be used with which table.
  5. Add column styles to the table styles you create. To do so, within the DataGridTableStyle Collection Editor, select the GridColumnStyle property and click the ellipsis button (Aa984371.vbellipsesbutton(en-us,VS.71).gif).

    The DataGridColumnStyle Collection Editor opens. In this dialog box, 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.

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.

    ' Visual Basic
    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 
    
    // C#
    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);
    }
    
    // C++
    private:
       void addCustomDataTableStyle()
       {
          // Add a GridTableStyle and set the MappingName 
          // to the name of the DataTable.
          DataGridTableStyle* TSAuthors = new DataGridTableStyle();
          TSAuthors->MappingName = S"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 = S"AV_FName";
          TCFirstName->HeaderText = S"First Name";
          TCFirstName->Width = 75;
          TSAuthors->GridColumnStyles->Add(TCFirstName);
    
          // Add the DataGridTableStyle instance to 
          // the GridTableStylesCollection. 
          dataGrid1->TableStyles->Add(TSAuthors);
       }
    

See Also

Deleting Columns in the Windows Forms DataGrid Control | GridTableStylesCollection Class | GridColumnStylesCollection Class | DataGrid Control (Windows Forms) | DataGrid Class

Show:
© 2014 Microsoft