如何使用行样式模板自定义 Windows 窗体 DataGridView 控件中的行

DataGridView 控件使用行模板作为它通过数据绑定添加到控件的所有行的基础,或者在调用 DataGridViewRowCollection.Add 该方法时不指定要使用的现有行。

行模板比RowsDefaultCellStyle属性能更好地控制行的外观和行为。 使用行模板,可以设置任何 DataGridViewRow 属性,包括 DefaultCellStyle

在某些情况下,必须使用行模板来实现特定效果。 例如,行高度信息不能存储在一个 DataGridViewCellStyle行中,因此必须使用行模板来更改所有行使用的默认高度。 创建了继承自 DataGridViewRow 的类时,若希望在向控件添加新行时使用自定义类型,行模板也很有用。

注释

仅当添加行时,才使用行模板。 无法通过更改行模板来更改现有行。

使用行模板

  • 设置从 DataGridView.RowTemplate 属性中检索到的对象的属性。

    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;
    
    With Me.dataGridView1.RowTemplate
        .DefaultCellStyle.BackColor = Color.Bisque
        .Height = 35
        .MinimumHeight = 20
    End With
    

编译代码

此示例需要:

另请参阅