简体   繁体   English

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

[英]Change color of specific part of DataGridView or entire column in C#

Is it possible to change the color of this part of a DataGridView by code? 是否可以通过代码更改DataGridView的此部分的颜色?

在此处输入图片说明

If not, how can I change the color of the whole DataGridViewRow ? 如果没有,如何更改整个DataGridViewRow的颜色? I tried this code, but it does not change the color: 我尝试了这段代码,但是它不会改变颜色:

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

As information, my columns are DataGridViewComboBoxColumn 作为信息,我的列是DataGridViewComboBoxColumn

You can set the row colour by setting DefaultCellStyle property. 您可以通过设置DefaultCellStyle属性来设置行颜色。 In the following example we are iterating through each row and checking cell[0] value and if the condition is true then setting the row backcolor and forecolor 在下面的示例中,我们遍历每行并检查cell [0]的值,如果条件为true,则设置行的backcolor和forecolor

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;
    }
}

if you means to say changing the blue background ,Blue background is the default color for a selected row in gridview. 如果您要说更改蓝色背景 ,则蓝色背景是gridview中选定行的默认颜色。 You can change this color within the properties window 您可以在属性窗口中更改此颜色

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

but if you are refreshing grid view in very short duration say less than 1 sec duration in that case you have to change the default color of added rows cells in Gridview,(above senario will work fine for static grid view). 但是,如果您要在很短的时间内刷新网格视图,请说少于1秒,那么在这种情况下,您必须更改Gridview中添加的行单元格的默认颜色(高于senario可以在静态网格视图中正常使用)。

Gridview1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Red;  or Color.Transparent 
Gridview1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;or Color.Transparent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM