DevExpress – Coloring individual cells or rows in a Gridcontrol based on a cell value

DevExpress – Coloring individual cells or rows in a Gridcontrol based on a cell value. This sample hooks into the RowCellStyle event of the Gridview, and evaluates the value of the cell. Depending on the outcome, a background color is assigned to the cell.

C# Sample

This sample hooks into the RowCellStyle event of the Gridview, and evaluates the value of the cell. Depending on the outcome, a background color is assigned to the cell.

private void gridViewQueue_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{

var oView = (GridView)sender;
if (e.Column.FieldName == "task_stamp")
{
Color oBgcolor;
var dtValue = DateTime.Parse(oView.GetRowCellValue(e.RowHandle, "task_stamp").ToString());
if (dtValue < DateTime.Now)
{
oBgcolor = Color.LightGreen;
}
else
{
oBgcolor = Color.LightYellow;
}

if (dtValue.Hour == 20)
{
oBgcolor = Color.Yellow;
}

e.Appearance.BackColor = oBgcolor;

}

}

To change the color of the full row, the check on the column is removed, while still querying the content of the specific field in the row. This way, the entire row is colored.

private void gridViewQueue_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{

var oView = (GridView)sender;
Color oBgcolor;
var dtValue = DateTime.Parse(oView.GetRowCellValue(e.RowHandle, "task_stamp").ToString());
if (dtValue < DateTime.Now)
{
oBgcolor = Color.LightGreen;
}
else
{
oBgcolor = Color.LightYellow;
}

if (dtValue.Hour == 20)
{
oBgcolor = Color.Yellow;
}
e.Appearance.BackColor = oBgcolor;

}

One Response to “DevExpress – Coloring individual cells or rows in a Gridcontrol based on a cell value”

  1. norman Says:

    Have you found a way to “trigger” CustomCellDraw or RowCellStyle event?
    I am trying to setup individual cell background but this should on be done after another task on thread is completed, thus I can’t do this from CustomCellDraw
    Any suggestion?

Leave a Reply

*