Udostępnij za pośrednictwem


How to: Use the Row Template to Customize Rows in the Windows Forms DataGridView Control

The DataGridView control uses the row template as a basis for all rows that it adds to the control either through data binding or when you call the DataGridViewRowCollection.Add method without specifying an existing row to use.

The row template gives you greater control over the appearance and behavior of rows than the RowsDefaultCellStyle property provides. With the row template, you can set any DataGridViewRow properties, including DefaultCellStyle.

There are some situations where you must use the row template to achieve a particular effect. For example, row height information cannot be stored in a DataGridViewCellStyle, so you must use a row template to change the default height used by all rows. The row template is also useful when you create your own classes derived from DataGridViewRow and you want your custom type used when new rows are added to the control.

Note

The row template is used only when rows are added. You cannot change existing rows by changing the row template.

To use the row template

  • Set properties on the object retrieved from the DataGridView.RowTemplate property.

    With Me.dataGridView1.RowTemplate
        .DefaultCellStyle.BackColor = Color.Bisque
        .Height = 35
        .MinimumHeight = 20
    End With
    
    DataGridViewRow row = this.dataGridView1.RowTemplate;
    row.DefaultCellStyle.BackColor = Color.Bisque;
    row.Height = 35;
    row.MinimumHeight = 20;
    
    DataGridViewRow^ row = this->dataGridView1->RowTemplate;
    row->DefaultCellStyle->BackColor = Color::Bisque;
    row->Height = 35;
    row->MinimumHeight = 20;
    
    

Compiling the Code

This example requires:

See Also

Reference

DataGridView

DataGridViewCellStyle

DataGridViewRow

DataGridView.RowTemplate

Concepts

Cell Styles in the Windows Forms DataGridView Control

Other Resources

Basic Formatting and Styling in the Windows Forms DataGridView Control