【问题标题】:Datagrid - how to change color of complete row?Datagrid - 如何更改完整行的颜色?
【发布时间】:2015-01-11 18:11:30
【问题描述】:

我有一个包含数据网格的 windows 窗体。 目前我有事件“dataGrid_CellFormatting”,它检查单元格的内容是否包含单词 FAIL 并将该单元格的颜色更改为红色。这行得通。 我需要更改什么才能将整个行更改为红色并且只有单元格?

谢谢

    private void dataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (dataGrid.Columns[e.ColumnIndex].Name.Equals("cResult"))
        {
            if ((String)e.Value == "FAIL")
            {
                e.CellStyle.BackColor = Color.Red;
            }
        }
    }

【问题讨论】:

标签: c# datagrid colors


【解决方案1】:

您为什么不只是更改行中的所有单元格?

private void dataGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGrid.Columns[e.ColumnIndex].Name.Equals("cResult"))
    {
        if ((String)e.Value == "FAIL")
        {
            foreach (DataGridViewCell cell in dataGrid.Rows[e.RowIndex].Cells)
            {
                cell.Style.BackColor = Color.Red;
            }   
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 2012-01-07
    • 2011-05-11
    • 1970-01-01
    • 2017-11-15
    • 2010-12-03
    • 1970-01-01
    • 2015-08-22
    • 2017-04-27
    相关资源
    最近更新 更多