Udostępnij za pośrednictwem


How to: Set Alternating Row Styles for the Windows Forms DataGridView Control Using the Designer

Tabular data is often presented in a ledger-like format where alternating rows have different background colors. This format makes it easier for users to tell which cells are in each row, especially with wide tables that have many columns.

With the DataGridView control, you can specify complete style information for alternating rows. You can use style characteristics like foreground color and font, in addition to background color, to differentiate alternating rows. For more information, see Cell Styles in the Windows Forms DataGridView Control.

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

Note

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 Working with Settings.

Define styles for alternating rows

  1. Select the DataGridView control in the designer.

  2. In the Properties window, click the ellipsis button (VisualStudioEllipsesButton screenshot) next to the AlternatingRowsDefaultCellStyle property.

  3. In the CellStyle Builder dialog box, define the style by setting the properties, and use the Preview pane to confirm your choices. The styles you specify are used for every other row displayed in the control, starting with the second one.

  4. To define styles for the remaining rows, repeat steps 2 and 3 using the RowsDefaultCellStyle property.

    Note

    Cells are displayed using styles inherited from multiple properties. For more information about style inheritance, see Cell Styles in the Windows Forms DataGridView Control.

See Also

Tasks

How to: Create a New Windows Forms Application Project

How to: Add Controls to Windows Forms

Reference

DataGridView

Concepts

Cell Styles in the Windows Forms DataGridView Control

Other Resources

Basic Formatting and Styling in the Windows Forms DataGridView Control

Using the Designer with the Windows Forms DataGridView Control