Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 274
Lavy Avatar asked Jun 29 '12 13:06

Lavy


People also ask

How to change cell color in DevExpress GridView in c#?

In the "Properties" tab, select a visualization effect (color scale, data bar, etc.), the column whose values you need to analyze and the column whose cells to paint. Disclaimer: The information provided on DevExpress.com and its affiliated web properties is provided "as is" without warranty of any kind.

How do I change the selected row color in DevExpress grid?

Answers approved by DevExpress Support SelectedRow property is used if the View's multi-select functionality is enabled. If you wish to change the background color of a focused row/cell, use the GridView. Appearance. FocusedRow/GridView.

How do I add a column in Gridcontrol DevExpress?

To add such columns manually, invoke the Data Grid designer, switch to the “Columns” tab and click the “Show Field List” button, then drag a data field into the column list. Data fields that have no corresponding grid columns are highlighted with bold text.


1 Answers

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

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

like image 137
SidAhmed Avatar answered Oct 27 '22 12:10

SidAhmed