RadGridView Row Formatting

10 posts, 0 answers
  1. George Georgiou
    George Georgiou avatar
    9 posts
    Member since:
    Sep 2004

    Posted 14 Feb 2008 Link to this post

    Hi,
    I need to manually set coloring in my grid rows(I don't want this, the customer does!!!) and I am puzzled

    I managed to do it with the header like this:

     For i As Integer = 0 To Me.RadGridView1.MasterGridViewInfo.TableHeaderRow.Cells.Count - 1
            Me.RadGridView1.MasterGridViewInfo.TableHeaderRow.Cells(i).CellElement.BackColor = Color.Green
            Me.RadGridView1.MasterGridViewInfo.TableHeaderRow.Cells(i).CellElement.BackColor2 = Color.Green
            Me.RadGridView1.MasterGridViewInfo.TableHeaderRow.Cells(i).CellElement.BackColor3 = Color.Green
            Me.RadGridView1.MasterGridViewInfo.TableHeaderRow.Cells(i).CellElement.BackColor4 = Color.Green
            Me.RadGridView1.MasterGridViewInfo.TableHeaderRow.Cells(i).CellElement.ForeColor = Color.White
          Next

    How can I do the same for the rest of the rows, especially when I need to have different colors for the alternating rows??


    Thank you in advance


    George


  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 14 Feb 2008 Link to this post

    Hi George Georgiou,

    Thank you for writing us.

    The preferred way to style the RadGridView is to modify a theme through the Visual Style Builder. You can find more about the Visual Style Builder from our online tutorials located here:

    http://www.telerik.com/support/videos/videos-list/b220i-kcd.aspx

    To implement alternating rows in RadGridView add a condition and use the IsOdd property of the GridRowElement and the IsOddRow of the GridCellElement.

    Another approach is to use the RowFormatting and CellFormatting events of RadGridView and modify the cell's appearance through code. Refer to the example below:

    this.radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting); 
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e) 
        e.RowElement.DrawFill = true
        if (e.RowElement.IsCurrent) 
        { 
            e.RowElement.BackColor = Color.FromArgb(0x10, 0xC0, 0xFF); 
        } 
        else 
        { 
            if (e.RowElement.IsOdd) 
            { 
                e.RowElement.BackColor = Color.FromArgb(0xC0, 0xC0, 0xC0); 
            } 
            else 
            { 
                e.RowElement.BackColor = Color.White; 
            } 
        } 
     

    Do not hesitate to contact us if you need any further assistance.

    Best wishes,
    Jack
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Ron Coffee
    Ron Coffee avatar
    4 posts
    Member since:
    Apr 2008

    Posted 24 Apr 2008 Link to this post

    I found that when you scroll the grid this event fires again.  The end result is that all the rows end up the same color.  I would recommend added an else, and format the even rows ro the default color.  This will maintain the alternating row colors.

    if

    (e.RowElement.IsOdd)

    {

    e.RowElement.DrawFill =

    true;

    e.RowElement.BackColor =

    Color.LightGray;

    }

    else

    {

    e.RowElement.DrawFill =

    true;

    e.RowElement.BackColor =

    Color.White ;

    }

  4. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 24 Apr 2008 Link to this post

    Hello Ron,

    Thank you for writing.

    You are correct, the RowFormatting event is fired every time when a visual element needs to change its state. This is because RadGridView uses virtualization of its elements. Only the rows that are visible on screen have visual elements. When the grid is scrolled, the visual elements are reused for other logical elements.

    Let me know, if you have other questions.
     

    Regards,
    Jack
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Tedd
    Tedd avatar
    2 posts
    Member since:
    Feb 2008

    Posted 14 Nov 2008 Link to this post

    Hi,
    Is it possible to change row color dynamically?
    for e.g. I want to change background color of row in following loop where i am checking if item is updated then change background color of row?
     foreach (Telerik.WinControls.UI.GridViewDataRowInfo gvDRI in radGridViewSelected.Rows)  
                        {  
                            if (gvDRI.Cells.Count > 0 && gvDRI.Cells["ClientName"].Value.ToString() == "Test")  
                            {  
                                gvDRI.Cells["IP"].Value = cs.Value.IP.ToString();  
                                //Change row color here?  
                            }  
     
                        } 

    We are using Q3 2008 for winforms.


    Best Regards
    Imran Tamboli
  6. Nikolay
    Admin
    Nikolay avatar
    1805 posts

    Posted 18 Nov 2008 Link to this post

    Hi Tedd,

    It is possible to change the row color. However, you should do this in the RowFormatting event handler, but not in a for loop. For additional information, please refer to the following online help article: Formatting Rows.

    If you have additional questions, feel free to contact me.

    Sincerely yours,
    Nikolay
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. Juan Stoppa
    Juan Stoppa avatar
    1 posts
    Member since:
    Aug 2009

    Posted 07 Oct 2009 Link to this post

    Hi,

    I'm trying to do something similar to what Tedd wanted to do.

    1) I'd like to be able to change the color of the row based on a value of one of its cell,
    for instance, if I have the table below:

    header ----  Name       -    Status       -  Due Date
    ---------------------------------------------------------------
    row 1    ----  Peter       -    Active      -   10/10/2009
    row 2    ----  Ben          -    Inactive   -   11/09/2009
    row 3    ----  Russell     -    Inactive   -   11/08/2009
    row 4    ----  Paul         -    Active      -   11/10/2009
    row 5    ----  Mark        -    Inactive     -  11/08/2009

    ...I'd like to set the color to red to those rows where the due date is after 01/10/2009 and green for the other rows so for the example above, the row 1 and 4 should be red and the row 2,3 and 5 should be green.

    I also have to take into consideration that the grid is going to be grouped by Status.

    I tried to use the code on the Formatting Rows article (http://www.telerik.com/help/winforms/formatting_rows.html) but I can't figure out how to do it.

    2) I noticed that the event "void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)"  is triggered when expanding a group, how can I trigger this event when loading the grid?

    Thanks in advance

    Juan
  8. Robert
    Robert avatar
    82 posts
    Member since:
    Jun 2009

    Posted 08 Oct 2009 Link to this post

    Hello Juan,

    Here is some example code specific to what you are trying to accomplish:

            private void radGridView1_ViewRowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e) 
            { 
                if (e.RowElement.RowInfo.Cells["Date"].Value != null
                { 
                    if (((DateTime)e.RowElement.RowInfo.Cells["Date"].Value).CompareTo(new DateTime(2009, 10, 1)) > 0) 
                    { 
                        e.RowElement.BackColor = Color.Red; 
                        e.RowElement.DrawFill = true
                    } 
                    else 
                    { 
                        e.RowElement.BackColor = Color.Green; 
                        e.RowElement.DrawFill = true
                    } 
                } 
            } 

    In creating this example, I used DataBinding and it appears that the RowFormatting event is fired when each row in the grid is created/loaded. Is this event not getting fired for you at the right times?

    I hope this helps.

    - Robert
  9. gezim
    gezim avatar
    19 posts
    Member since:
    Aug 2012

    Posted 10 Aug 2011 Link to this post

    e.RowElement.BackColor = Color.     ????
    I can't use this Color.anycolor 
  10. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 11 Aug 2011 Link to this post

    Hi Gezim,

    Thank you for writing.

    Yes, you can set any color you want. For more information about the RowFormatting event, please refer to the following help article.

    I hope this helps.

    Greetings,
    Stefan
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Back to Top