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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11262946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:10:41  来源:igfitidea点击:

How to change background color of a cell in Devexpress Grid?

c#griddevexpressxtragrid

提问by Lavy

I have a devexpress xtragrid with 40 columns. I compare each cell value with other and if it is different then I want to change the cell background color. I try with GridViewInfo but it only takes the columns that are visible on the screen.But I want to do for all the columns.(Not with RowCellStyle) Do you have a solution for that? Thank you!

我有一个带有 40 列的 devexpress xtragrid。我将每个单元格值与其他值进行比较,如果不同,那么我想更改单元格背景颜色。我尝试使用 GridViewInfo 但它只需要屏幕上可见的列。但我想为所有列做。(不是 RowCellStyle)你有解决方案吗?谢谢!

采纳答案by Dave New

Hook onto the RowStyle event of your xtragrid.

连接到 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;
        }
    }
}

回答by SidAhmed

You need to handle the CustomDrawCellof your GridView, here is a snip of code that change the color of the Name column, based on an other column valoe (age column)

您需要处理GridView的CustomDrawCell,这里是一段代码,根据其他列值(年龄列)更改名称列的颜色

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

Good luck

祝你好运

回答by Krishnakumar