如何在Devexpress Grid中更改单元格的背景颜色?

7

我有一个包含40列的DevExpress XtraGrid表格。我需要比较每个单元格的值,如果不同则改变单元格的背景颜色。我尝试使用GridViewInfo,但它只能获取屏幕上可见的列。但我想对所有列执行此操作(而不是使用RowCellStyle)。请问您有解决方案吗?谢谢!

3个回答

7
您需要处理GridView的CustomDrawCell,以下是一段代码片段,可以根据另一列的值(年龄列)更改Name列的颜色。
private void gridView_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
    {
        if (e.Column == colName)
        {
            var age = Convert.ToInt32(gridView.GetRowCellValue(e.RowHandle, colAge));
            if (age < 18)
                e.Appearance.BackColor = Color.FromArgb(0xFE, 0xDF, 0x98);
            else
                e.Appearance.BackColor = Color.FromArgb(0xD2, 0xFD, 0x91);
        }
    }

祝你好运


5

在您的XtraGrid上挂钩RowStyle事件。

private void maintainDataControl_RowStyle(object sender, RowStyleEventArgs e)
{
    if (e.RowHandle >= 0)
    {
        GridView view = sender as GridView;

        // Some condition
        if((string)view.GetRowCellValue(
            e.RowHandle, view.Columns["SomeRow"]).Equals("Some Value"))
        {
            e.Appearance.BackColor = Color.Green;
        }
    }
}

比较函数在一个按钮上。我如何调用RowStyle事件? - Lavy
你不能在按钮点击事件中执行此操作。你必须处理RowStyle或者CustomDrawCell。将它们的条件放在那里,然后在对数据进行更改后,简单地使网格无效即可。 - Niranjan Singh

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接