DataGridViewCell.Style 属性


获取或设置单元格的样式。Gets or sets the style for the cell.

 property System::Windows::Forms::DataGridViewCellStyle ^ Style { System::Windows::Forms::DataGridViewCellStyle ^ get(); void set(System::Windows::Forms::DataGridViewCellStyle ^ value); };
public System.Windows.Forms.DataGridViewCellStyle Style { get; set; }
member this.Style : System.Windows.Forms.DataGridViewCellStyle with get, set
Public Property Style As DataGridViewCellStyle


与单元格关联的样式。The style associated with the cell.



下面的代码示例演示如何使用 Style 属性获取单元的背景色。The following code example demonstrates how to use the Style property to get the background color of a cell. 此示例摘自为 DataGridViewColumn 类提供的更大的代码示例。This example is part of a larger code example provided for the DataGridViewColumn class.

void CustomizeCellsInThirdColumn()
   int thirdColumn = 2;
   DataGridViewColumn^ column = dataGridView->Columns[ thirdColumn ];
   DataGridViewCell^ cell = gcnew DataGridViewTextBoxCell;
   cell->Style->BackColor = Color::Wheat;
   column->CellTemplate = cell;

private void CustomizeCellsInThirdColumn()
    int thirdColumn = 2;
    DataGridViewColumn column =
    DataGridViewCell cell = new DataGridViewTextBoxCell();

    cell.Style.BackColor = Color.Wheat;
    column.CellTemplate = cell;
Private Sub CustomizeCellsInThirdColumn()

    Dim thirdColumn As Integer = 2
    Dim column As DataGridViewColumn = _
    Dim cell As DataGridViewCell = _
        New DataGridViewTextBoxCell()

    cell.Style.BackColor = Color.Wheat
    column.CellTemplate = cell
End Sub


DataGridView 控件使用单元格 InheritedStyle 属性指示的样式显示其单元格,该属性从 DataGridViewCellStyle类型的其他属性继承样式。The DataGridView control displays its cells using the styles indicated by the cell InheritedStyle property, which inherits styles from other properties of type DataGridViewCellStyle. 通过 Style 属性指定的样式将覆盖通过所有其他单元格样式属性指定的样式,但不一定表示对该单元格的外观构成的所有样式。The styles specified through the Style property override the styles specified through all other cell-style properties, but do not necessarily indicate all the styles that contribute to the cell's appearance.

有关详细信息,请参阅Windows 窗体 DataGridView 控件中的单元格样式For more information, see Cell Styles in the Windows Forms DataGridView Control.