This is a migrated thread and some comments may be shown as answers.

Grid View

2 Answers 51 Views
GridView
This is a migrated thread and some comments may be shown as answers.
balasubramanyam
Top achievements
Rank 1
balasubramanyam asked on 20 Nov 2012, 07:08 AM
How to change Style.BackColor property for particular Cell?

2 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 20 Nov 2012, 10:03 AM
Hello,

Just handle the CellFormatting event, check for the column and the specific cell and set the backcolor, like so:
void grid_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.Column.Name == "SomeColumn")
    {
        var dataBoundObject = e.CellElement.RowInfo.DataBoundItem;
        // check if this matches your case
        e.CellElement.BackColor = Color.Red;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
    }
}

You have to keep in mind that because the grid reuses cell (just the displayed cells are created and drawn), you will have to reset the custom properties you set on the cell.


Best Regards,
Emanuel Varga
WinForms MVP
0
Plamen
Telerik team
answered on 22 Nov 2012, 03:43 PM
Hi guys,

Cells and rows can be styled by changing the data cells back color:
e.CellElement.BackColor = Color.Red;
e.CellElement.BackColor = Color.Green;
e.CellElement.BackColor = Color.Blue;

CellFormatting
event is used to add formatting to grid data cells including the new row cells.
While CellFormatting event is fired only for data cells, ViewCellFormatting is fired for all RadGridView cells. So if you want to format the grouping row or the header cells, you should use the ViewCellFormatting event.

More information about the formatting abilities of the cells can be found in these documentation article:

I hope this helps. Should you have any other questions, I will be glad to assist you.

Greetings,
Plamen
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
balasubramanyam
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or