【问题标题】:C# DataGridView BackColor Not OverwrittenC# DataGridView BackColor 未被覆盖
【发布时间】:2018-03-02 23:24:33
【问题描述】:

我的表单申请中有DataGridView。从数据库中的表中检索数据并在DataGridView 中显示它们后,如果满足特定条件,我将green 颜色应用于某些单元格的行的BackColor。 在这些单元格被着色为 green 之后,程序会让它们通过另一个条件,如果它们不能满足健康)状况。

但是,似乎预着色的单元格不能用新颜色覆盖。 即使我应用以下代码为整行着色红色,它也仅适用于未预先着色的单元格。

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

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

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

我想知道是否有更好的方法来覆盖背景颜色。有人可以帮忙吗?

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

第一个条件失败的人会自动将整行变为红色,这很有效。但是,如果他们通过第一个条件并将其“Passed1”单元格变为绿色,然后不符合第二个条件,如您所见,这些单元格将保持绿色。我想将整行着色为红色,甚至将预着色为绿色的单元格覆盖为红色。

【问题讨论】:

  • a) 解释“预着色”! b) 请参阅 here 以获取在 CellPainting 事件中为单元格着色的示例。如果您为全套条件编写代码就足够了。
  • 如果一个新的 BackColor 已应用到一个单元格(即预着色),则该单元格的 BackColor 不能更改为另一种颜色,即使我尝试使用 myDataGridView 为包括该单元格在内的整行着色。行[i].DefaultCellStyle.BackColor
  • 这将是默认值和覆盖值之间的区别。使用 cell.BackColor 覆盖默认值或先前的更改!另外:尝试将所有颜色等集中在 CellPainting 中。
  • 在我贴在帖子中的图片中,ID:2、4 和 5 的行不能完全着色为红色,它们的“Passed1”单元格保持绿色。
  • 好吧,如果您只是将单元格颜色更改为绿色,则只是更改了该单元格,而不是整行。请阅读How to Ask 并采取tour

标签: c# datagridview backcolor


【解决方案1】:

绘制单元格时,单元格背景颜色具有优先顺序。从顶部开始,它将沿列表向下级联,直到设置颜色*:

  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

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

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

结果:

【讨论】:

  • 谢谢!这正是我想要的答案。我不知道有优先顺序。那是导致我的问题的原因。很高兴您正确理解了我的问题。
  • 平心而论,我从来没有在任何地方找到过这个order 的列表——甚至在我搜索MSDN 文档时也没有。我只有通过反复试验才知道。
  • 我只是想再次感谢您。我正在开发我的新项目,遇到了与此非常相似的问题,我回到了这个优先顺序,它立即解决了这个问题。谢谢谢谢谢谢。
  • 太棒了!乐意效劳。 :)
  • @OhBeWise 也有类似的问题,我个人认为最新的没有覆盖它很奇怪。
【解决方案2】:

datagridview 控件存在一个问题,即在使用 DefaultCellStyle 属性显示表单之前,您无法更改任何单元格的颜色。因此,在调用 Shown() 之前运行的方法或触发的事件不会改变颜色。可能就是这个问题。可能你必须创建一个绘画方法并在 Shown() 之前调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多