How to set RadGrid Cell style dynamically

10 posts, 2 answers
  1. CJ
    CJ avatar
    6 posts
    Member since:
    Dec 2010

    Posted 13 Dec 2010 Link to this post

    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
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 13 Dec 2010 Link to this post

    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
  3. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 14 Dec 2010 Link to this post

    Hello,

    Did this help? If so, please remember to mark as answer
    Thanks
    Richard
  4. CJ
    CJ avatar
    6 posts
    Member since:
    Dec 2010

    Posted 14 Dec 2010 Link to this post

    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
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 14 Dec 2010 Link to this post

    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
  6. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 16 Dec 2010 Link to this post

    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.
  7. CJ
    CJ avatar
    6 posts
    Member since:
    Dec 2010

    Posted 16 Dec 2010 Link to this post

    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
  8. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 22 Dec 2010 Link to this post

    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.
  9. Adam
    Adam avatar
    38 posts
    Member since:
    Oct 2010

    Posted 18 Jan 2012 Link to this post

    Thanks for this post!  I searched and played around for an hour before finding this property...
  10. Nikolay
    Admin
    Nikolay avatar
    1805 posts

    Posted 20 Jan 2012 Link to this post

    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).

Back to Top