0

是否可以通过代码更改这部分的颜色DataGridView

在此处输入图像描述

如果没有,我怎样才能改变整体的颜色DataGridViewRow?我试过这段代码,但它不会改变颜色:

DataGridViewRow row = new DataGridViewRow();
row.DefaultCellStyle.BackColor = Color.Green;
row.DefaultCellStyle.ForeColor = Color.Green;

作为信息,我的专栏是DataGridViewComboBoxColumn

4

2 回答 2

1

您可以通过设置 DefaultCellStyle 属性来设置行颜色。在下面的示例中,我们遍历每一行并检查 cell[0] 值,如果条件为真,则设置行背景色和前景色

foreach (DataGridViewRow row in mydataGridView.Rows)
{
    string RowType = row.Cells[0].Value.ToString();

    if (RowType == "Some Value")
    {
        row.DefaultCellStyle.BackColor = Color.Green;
        row.DefaultCellStyle.ForeColor = Color.Green;
    }
}
于 2013-09-04T08:03:21.267 回答
0

如果你的意思是改变蓝色背景,蓝色背景是gridview中选定行的默认颜色。您可以在属性窗口中更改此颜色

 Gridview1.DefaultCellStyle.SelectionBackColor = Color.Red; or Color.Transparent
 Gridview1.DefaultCellStyle.SelectionForeColor = Color.Black; or Color.Transparent

但是,如果您在很短的时间内刷新网格视图,比如持续时间少于 1 秒,那么在这种情况下,您必须更改 Gridview 中添加的行单元格的默认颜色,(上面的 senario 将适用于静态网格视图)。

Gridview1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Red;  or Color.Transparent 
Gridview1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;or Color.Transparent
于 2013-09-05T12:15:20.930 回答