How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer

The DataGridView control lets you specify default cell styles and cell data formats for the entire control, for specific columns, for row and column headers, and for alternating rows to create a ledger effect. Default styles set for the entire control are overridden by default styles set for columns and alternating rows. Additionally, styles that you set in code for individual rows and cells override the default styles.

For more information about cell styles, see Cell Styles in the Windows Forms DataGridView Control. To set styles for alternating rows, see How to: Set Alternating Row Styles for the Windows Forms DataGridView Control Using the Designer.

You can also set styles using the RowTemplate property to affect all rows that will be added to the control. For more information about the row template, see How to: Use the Row Template to Customize Rows in the Windows Forms DataGridView Control.

The following procedures require a Windows Application project with a form containing a DataGridView 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.

To set default styles for all cells in the control

  1. Select the DataGridView control in the designer.

  2. In the Properties window, click the ellipsis button (The Ellipsis button (...) in the Properties window of Visual Studio.) next to the DefaultCellStyle, ColumnHeadersDefaultCellStyle, or RowHeadersDefaultCellStyle property. The CellStyle Builder dialog box appears.

  3. Define the style by setting the properties, using the Preview pane to confirm your choices.

Note

If visual styles are enabled, the row and column headers (except for the TopLeftHeaderCell) are automatically styled by the current theme, overriding the ColumnHeadersDefaultCellStyle and RowHeadersDefaultCellStyle property values.

You can set cell styles for multiple selected DataGridView controls using the designer, but only if they have identical values for the cell style property you want to modify. If any cell styles differ for that property, the Properties windows of the CellStyle Builder dialog box will be blank.

To set default styles for cells in individual columns

  1. Right-click the DataGridView control in the designer and choose Edit Columns.

  2. Select a column from the Selected Columns list.

  3. In the Column Properties grid, click the ellipsis button (The Ellipsis button (...) in the Properties window of Visual Studio.) next to the DefaultCellStyle property. The CellStyle Builder dialog box appears.

  4. Define the style by setting the properties, using the Preview pane to confirm your choices.

To format data in cells

  1. Use one of the preceding procedures to display a CellStyle Builder dialog box related to a default cell style property.

  2. In the CellStyle Builder dialog box, click the ellipsis button (The Ellipsis button (...) in the Properties window of Visual Studio.) next to the Format property. The Format String dialog box appears.

  3. Select a format type, then modify the details of the type (such as the number of decimal places to display), using the Sample box to confirm your choices.

  4. If you are binding the DataGridView control to a data source that is likely to contain null values, fill in the Null Value text box. This value is displayed when the cell value is equal to a null reference (Nothing in Visual Basic) or DBNull.Value.

See also