简体   繁体   中英

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?

在此处输入图片说明

If not, how can I change the color of the whole 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

You can set the row colour by setting DefaultCellStyle property. 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

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. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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