如何以编程方式更改绑定到具有自定义类的BindingSource的DataGridView中的列的颜色?

时间:2012-02-15 19:56:55

标签: c# .net winforms datagridview

BindingSource的DataSource是一个具有一些公共属性的类。网格的列是这样创建的:

DataGridViewTextBoxColumn vendorIDColumn =
           new DataGridViewTextBoxColumn()
           {
               MinimumWidth = 50,
               FillWeight = 20,
               DataPropertyName = "VendorID",
               HeaderText = "Vendor ID",
               AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
           };
        grid.Columns.Add(vendorIDColumn);

DataGridView的属性“AutoGenerateColumns”设置为“false”。 尝试grid.Columns[0].DefaultCellStyle.BackColor = Color.Red;,但没有用。

1 个答案:

答案 0 :(得分:0)

您可以设置DataGridViewTextBoxColumn的背景颜色吗?

DataGridViewTextBoxColumn vendorIDColumn =
       new DataGridViewTextBoxColumn()
       {
           MinimumWidth = 50,
           FillWeight = 20,
           DataPropertyName = "VendorID",
           HeaderText = "Vendor ID",
           AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
           BackColor = Color.Red;
       };
    grid.Columns.Add(vendorIDColumn);