DataGridViewColumn.CellTemplate 属性

定义

获取或设置用于创建新单元格的模板。

C#
[System.ComponentModel.Browsable(false)]
public virtual System.Windows.Forms.DataGridViewCell CellTemplate { get; set; }
C#
[System.ComponentModel.Browsable(false)]
public virtual System.Windows.Forms.DataGridViewCell? CellTemplate { get; set; }

属性值

一个 DataGridViewCell,列中的所有其他单元格都以它为模型。 默认值为 null

属性

示例

下面的代码示例演示如何使用 DataGridViewCell 作为 的 DataGridViewColumn模板。 对列中的任何单元格所做的样式更改会影响列的所有单元格。 此代码示例是为 DataGridViewColumn 类提供的一个更大示例的一部分。

C#
private void CustomizeCellsInThirdColumn()
{
    int thirdColumn = 2;
    DataGridViewColumn column =
        dataGridView.Columns[thirdColumn];
    DataGridViewCell cell = new DataGridViewTextBoxCell();

    cell.Style.BackColor = Color.Wheat;
    column.CellTemplate = cell;
}

注解

类的 DataGridViewColumn 构造函数初始化此属性。 无参数构造函数将 属性设置为 null;另一个构造函数从其 参数复制单元格模板。

备注

更改单元格模板的属性不会立即影响用户界面 (UI) 列的现有单元格。 只有在重新生成列后,这些更改才明显 (例如,通过对列进行排序或通过 DataGridView.InvalidateColumn 调用方法) 。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅