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

How to set RadGrid Cell style dynamically

9 Answers 1091 Views
GridView
This is a migrated thread and some comments may be shown as answers.
CJ
Top achievements
Rank 1
CJ asked on 13 Dec 2010, 11:24 PM
Hi,

I want to set the style of a grid cell at runtime. I am passing the radgridview control as a reference to a helper class and based on some values i am setting the background color of a cell as follows, but it does not work.

radGrid.Rows[1].Cells[1].Style.BackColor = System.Drawing.Color.Red;

Whereas setting the ForeColor the same way works.

radGrid.Rows[1].Cells[1].Style.ForeColor = System.Drawing.Color.Red;


Please advice.
thanks

9 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 14 Dec 2010, 12:46 AM
Hello,

You should use the CellFormatting event to style cells. please take a look at this link which explains CellFormatting in detail.
Hope this helps but let me know if yo uhave further questions
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 14 Dec 2010, 03:41 PM
Hello,

Did this help? If so, please remember to mark as answer
Thanks
Richard
0
CJ
Top achievements
Rank 1
answered on 14 Dec 2010, 03:47 PM
Hi,

Thank you for your response. My problem here is different. I am not doing the style in the code behind file. I am passing the grid as a reference to another helper class and setting the styles there based on few conditions. I will not be able to raise this CellFormattingEvent.

thanks
0
Richard Slade
Top achievements
Rank 2
answered on 14 Dec 2010, 03:56 PM
Hello,

The Cellformatting event is the place where you will need to do this because the RadgridView uses UI Virtualization. This means that the cells are re-used for the data as it scrolls and as values are changed.
Cell elements are created only for currently visible cells and are reused during operations like scrolling, filtering, grouping and so on. This approach improves greatly the performance of the control.
 
Have a look at this article which further explain the logical vs visual structure of the RadGridView. You need to get to the CellElement property in order to set the properties that you want to. The place to do this is the CellFormatting event.

Hope that helps but let me know if you need more information
Richard
0
Accepted
Svett
Telerik team
answered on 16 Dec 2010, 05:34 PM
Hello Sagayanathan,

If you need to customize the background of cell via the Style property, you should set the CustomizeFill property to true.

this.radGridView1.Rows[1].Cells[1].Style.BackColor = System.Drawing.Color.Red;
this.radGridView1.Rows[1].Cells[1].Style.DrawFill = true;
this.radGridView1.Rows[1].Cells[1].Style.CustomizeFill = true;

In addition, you can achieve your scenario by using the CellFormatting event that Richard has mentioned.

Greetings,
Svett
the Telerik team
Check out the Q1 2011 Roadmap for Telerik Controls for Windows Forms.
0
CJ
Top achievements
Rank 1
answered on 17 Dec 2010, 04:14 AM
Hi Svett 
Thank you very much for your answer on setting cell backcolor dynamically, its working fine. 
Similary i want to know how to set the gradient for a cell dynamically, i am trying the following but it does not work.

radGrid.Rows[0].Cells[1].Style.GradientPercentage = 50f;
radGrid.Rows[0].Cells[1].Style.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
radGrid.Rows[0].Cells[1].Style.GradientAngle = 180f;


thanks
J Sagayanathan
0
Accepted
Svett
Telerik team
answered on 22 Dec 2010, 10:00 AM
Hi Sagayanathan,

You should change the GradientStyle to have a value different than GradientStyles.Solid. You can use the following code snippet as a sample:

1.this.radGridView1.Rows[1].Cells[1].Style.BackColor = System.Drawing.Color.Red;
2.this.radGridView1.Rows[1].Cells[1].Style.BackColor2 = System.Drawing.Color.Blue;
3.this.radGridView1.Rows[1].Cells[1].Style.BackColor3 = System.Drawing.Color.Yellow;
4.this.radGridView1.Rows[1].Cells[1].Style.GradientStyle = Telerik.WinControls.GradientStyles.Gel;
5.this.radGridView1.Rows[1].Cells[1].Style.DrawFill = true;
6.this.radGridView1.Rows[1].Cells[1].Style.CustomizeFill = true;

Best wishes,
Svett
the Telerik team
Check out the Q1 2011 Roadmap for Telerik Controls for Windows Forms.
0
Adam
Top achievements
Rank 1
answered on 18 Jan 2012, 09:36 PM
Thanks for this post!  I searched and played around for an hour before finding this property...
0
Nikolay
Telerik team
answered on 20 Jan 2012, 10:44 AM
Hello Adam,

Thank you for this input.

You can also take a look at the bottom of this article where the Style property is concerned.

Greetings,
Nikolay
the Telerik team

SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

Tags
GridView
Asked by
CJ
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
CJ
Top achievements
Rank 1
Svett
Telerik team
Adam
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or