C# 在C中更改DataGridView的特定部分或整个列的颜色#

C# 在C中更改DataGridView的特定部分或整个列的颜色#,c#,winforms,datagridview,styling,C#,Winforms,Datagridview,Styling,是否可以通过代码更改DataGridView这部分的颜色 如果没有,如何更改整个DataGridViewRow的颜色?我尝试了此代码,但它不会更改颜色: DataGridViewRow row = new DataGridViewRow(); row.DefaultCellStyle.BackColor = Color.Green; row.DefaultCellStyle.ForeColor = Color.Green; 作为信息,我的列是DataGridViewComboxColumn您

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

如果没有,如何更改整个
DataGridViewRow
的颜色?我尝试了此代码,但它不会更改颜色:

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

作为信息,我的列是
DataGridViewComboxColumn

您可以通过设置DefaultCellStyle属性来设置行颜色。在下面的示例中,我们迭代每一行并检查单元格[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;
    }
}

如果您想说更改蓝色背景,则蓝色背景是gridview中选定行的默认颜色。可以在“属性”窗口中更改此颜色

 Gridview1.DefaultCellStyle.SelectionBackColor = Color.Red; or Color.Transparent
 Gridview1.DefaultCellStyle.SelectionForeColor = Color.Black; or Color.Transparent
但是,如果您在很短的时间内刷新网格视图,比如说少于1秒的时间,那么您必须更改网格视图中添加的行单元格的默认颜色(上面的senario可以很好地用于静态网格视图)


你是说这个银色的?有可能,首先将EnableHeaderServicesAllStyles设置为false,然后在RowHeadersDefaultCellStyle中,您将获得BackColor.erem,谢谢。如果你加上这个作为答案,我会同意的。
Gridview1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Red;  or Color.Transparent 
Gridview1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;or Color.Transparent