C#DataGridView BackColor不被覆盖 [英] C# DataGridView BackColor Not Overwritten

查看:50
本文介绍了C#DataGridView BackColor不被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单应用程序上有 DataGridView .从数据库中的表中检索数据并在 DataGridView 中显示它们之后,我将绿色颜色应用到某些单元格的 BackColor 行中,如果满足一定条件.将这些单元格着色为绿色后,程序使它们经历另一种条件,如果它们不能满足要求,则会为整行的 BackColor 红色着色.状况.

I have DataGridView on my form application. After retrieving data from a table in the database and displaying them in DataGridView, I apply a green color to some cell's BackColor of the rows if a certain condition is met. After those cells are colored green, the program makes them go through another condition, which colors the whole row's BackColor red if they fail to satisfy the condition.

但是,似乎预着色的单元格无法用新的颜色覆盖.即使我应用以下代码为整个行红色着色,它也仅适用于尚未预着色的单元格.

However, it seems like pre-colored cells cannot be overwritten with a new color. Even if I apply the following code to color the whole row red, it only works for the cells that have not been pre-colored.

for(int i=0; i<myDataGridview.Rows.Count; i++){  
    if(/*a certain condition FAILS*/){
        myDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Red;
    }
}

现在,我正在为那些预先着色的单元格 red 逐个着色,但这需要花费大量时间和代码,例如:

Right now, I am coloring those pre-colored cells red one by one, but this takes a lot of time and code like:

for(int i=0; i<myDataGridview.Rows.Count; i++){  
    if(/*a certain condition FAILS*/){
        //Trying to color the whole row RED, but not working
        myDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Red;
        //Manually color the cells, which are pre-colored to green, RED
        myDataGridView.Rows[i].Cells[6].Style.BackColor = Color.Red;
        myDataGridView.Rows[i].Cells[7].Style.BackColor = Color.Red;
        ....
        myDataGridView.Rows[i].Cells[13].Style.BackColor = Color.Red;
        myDataGridView.Rows[i].Cells[16].Style.BackColor = Color.Red;
    }
}

我想知道是否有更好的方法可以覆盖backColor.有人可以帮忙吗?

I am wondering if there is a better way to override the backColor. Can someone please help?

这是一个示例(模仿)DataGridView.

Here is an example (imitation) DataGridView.

在第一个条件不及格的人会自动将其整个行变成红色,并且行得通.但是,如果它们通过了第一个条件,并且将其"Passed1"单元格设置为绿色,然后失败了第二个条件,如您所见,这些单元格将保持绿色.我想将整个行涂成红色,甚至将预先着色为绿色的单元格覆盖为红色.

Those who failed the first condition automatically get their whole row red, and that works. However, if they pass the first condition and get their "Passed1" cell colored green, and then fail the second condition, as you can see, those cells stay green. I want to color the whole row red, even overwriting the pre-colored-to-green cell to red.

推荐答案

绘制单元格时,单元格背景色具有优先级.从顶部开始,它将向下层叠列表,直到设置了颜色*:

Cell background colors have an order of precedence when the cell is drawn. Starting at the top, it will cascade down the list until the color is set*:

  1. Cell.Style.BackColor
  2. Row.DefaultCellStyle.BackColor
  3. DataGridView.AlternatingRowsDefaultCellStyle.BackColor
  4. DataGridView.RowsDefaultCellStyle.BackColor
  5. Column.DefaultCellStyle.BackColor
  6. DataGridView.DefaultCellStyle.BackColor
  1. Cell.Style.BackColor
  2. Row.DefaultCellStyle.BackColor
  3. DataGridView.AlternatingRowsDefaultCellStyle.BackColor
  4. DataGridView.RowsDefaultCellStyle.BackColor
  5. Column.DefaultCellStyle.BackColor
  6. DataGridView.DefaultCellStyle.BackColor

*此列表不广泛,可能不包括所有可能的BackColor可访问属性.

您可能正在执行类似于为 Passed1 列中的单元格设置 Cell.Style.BackColor 的操作,然后执行您发布的代码逻辑.给出的结果如您所见,因为 Green 的优先级高于设置它的 Red :

It is likely you are doing something akin to setting Cell.Style.BackColor for cells in the Passed1 column, then the code logic you posted. Which gives results like you're seeing because Green has a higher precedence than Red where it's set:

假设您通过两个 Passed 列的条件是二进制的(),则可以通过降低"优先级来解决此问题通过设置 Column.DefaultCellStyle.BackColor :

Assuming your conditions for the two Passed columns is binary (Yes or No), you can fix this by "lowering" the precedence of Green background by setting the Column.DefaultCellStyle.BackColor:

private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    this.dataGridView1.Columns["Passed1"].DefaultCellStyle.BackColor = Color.Green;
    this.dataGridView1.Columns["Passed2"].DefaultCellStyle.BackColor = Color.Green;

    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        if (row.Cells["Passed1"].Value.ToString() == "No" || row.Cells["Passed2"].Value.ToString() == "No")
        {
            row.DefaultCellStyle.BackColor = Color.Red;
        }
    }
}

这将导致:

这篇关于C#DataGridView BackColor不被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆