【问题标题】:Error in Coloring Datagridview Row in c#在 c# 中为 Datagridview 行着色时出错
【发布时间】:2014-06-18 15:49:54
【问题描述】:

我有一个 datageidview 应该为包含特定值的行着色

    private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        foreach (DataGridViewRow myrow in dataGridView2.Rows)
        {
            if (e.RowIndex != -1)
            {
                    if (myrow.Cells[7].Value.ToString() == "Error")
                    {
                        myrow.DefaultCellStyle.BackColor = Color.Red;
                    }
                    else if (myrow.Cells[7].Value.ToString() == "NoError")
                    {
                        myrow.DefaultCellStyle.BackColor = Color.Green;
                    }
                }
        }
    }

但是当第一行包含这个值时我遇到了一个问题,所有行都用它的颜色着色

有什么帮助吗??

【问题讨论】:

  • 如果单元格 7 的值为 Error 并且我在 datagridview_cellformating 中使用此代码,则假设将行设置为红色

标签: c# winforms datagridview


【解决方案1】:

为网格中的所有可见单元格发送 CellFormatting 事件。使用事件中提供的数据更改颜色可能会更好。

private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.RowIndex != -1)
    {
        if (dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString() == "Error")
        {
            e.CellStyle.BackColor = Color.Red;
        }
        else if (dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString() == "NoError")
        {
             e.CellStyle.BackColor = Color.Green;
        }
    }
}

【讨论】:

  • 这很好,但我想为整行着色,没有代码中的单元格任何更改??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
相关资源
最近更新 更多