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

CellFormatting

1 Answer 15 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 10 Apr 2014, 04:37 PM
Hi,

Apologies if this is a daft question, I usually get involved with web stuff.

We have a MatchLevelID Column in our RadGridView.
The MatchLevelID can either be 0, 1, 2 or 3
What we would like to do is set the background colour of the Cell depending on the MatchLevelID.
To do this, we are subscribing to the CellFormatting Event.
Here is the code we have so far :

private void rgv_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.Name == "MatchLevelID")
            {
                var cellInfo = e.CellElement.RowInfo.Cells[e.CellElement.ColumnIndex];

                var matchLevelID = (int) cellInfo.Value;

                switch (matchLevelID)
                {
                    case 0:
                        e.CellElement.BackColor = Color.Red;
                        break;
                    case 1:
                        e.CellElement.BackColor = Color.Orange;
                        break;
                    case 2:
                        e.CellElement.BackColor = Color.Yellow;
                        break;
                    case 3:
                        e.CellElement.BackColor = Color.Green;
                        break;
                }
            }
        }

What is happening is that the background colour is only being set in the MatchLevelID Cell for the Row that the Cursor is hovering over.
As soon as we move the Cursor to another Row the background colour for the new Row is changed and the colour for the old Row is lost.
Ideally we would like the colour to be set for all the Cells in the MatchLevelID Column when the Grid loads.

Any ideas ?

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 14 Apr 2014, 11:48 AM
Hello Chris,

Thank you for writing.

In order to be able to set the BackColor of a cell, you first have to set its DrawFill property to true. To change its gradient style, you should use the GradientStyle property - for example you can set it to Solid of you do not need gradients. 

In addition, it is very important that you reset all settings you introduce in the formatting events, in order to prevent undesired formatting. 

More information and examples of this is available here: http://www.telerik.com/help/winforms/gridview-cells-formatting-cells.html.

I hope this helps.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or