Changing the AlternatingRowColor of a Gridview Programmatically

7 posts, 0 answers
  1. Jeremy Murtishaw
    Jeremy Murtishaw avatar
    86 posts
    Member since:
    Apr 2009

    Posted 22 May 2009 Link to this post

    Hello,

    Thanks for all the answers to my questions thus far!

    This time I'm having trouble setting the alternativerowcolor of a gridview programmatically after setting the datasource. Essentially something like this would be ideal:

    radGridView1.TableElement.AlternatingRowColor = Color.Blue;

    However, I can see no way of getting to the table element from the gridview, even through the rootelement, mastergridviewtemplate and gridelement properties...

    Thanks for the help!
    Jeremy
  2. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 25 May 2009 Link to this post

    Hi Jeremy,

    You should cast the GridElement property to GridTableElement. You should additionally set the EnableAlternatingColor to true. Here is a sample:

    ((GridTableElement)this.radGridView1.GridElement).AlternatingRowColor = Color.Blue; 
    this.radGridView1.EnableAlternatingRowColor = true

    I hope this helps. If you have more questions, don't hesitate to ask.

    All the best,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Sheraz Naseeb
    Sheraz Naseeb avatar
    22 posts
    Member since:
    Mar 2010

    Posted 01 Sep 2010 Link to this post

    Hello Telerik Team,

    I am looking to do the same functionality for the child templates, I want to change the alternating row colour for child template inside the Gridview, please guide me if there is any way to do so. basically I need alternating colour different in both main grid and child template, lets say Beige in main grid and Lavender in child template.

    Many thanks,

    Sheraz
  4. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 02 Sep 2010 Link to this post

    Hi Sheraz Naseeb,

    This can be done by handling the ChildViewExpanded event. Here is a sample:

    this.radGridView1.ChildViewExpanded += new ChildViewExpandedEventHandler(radGridView1_ChildViewExpanded);
    this.radGridView1.EnableAlternatingRowColor = true;
    this.radGridView1.TableElement.AlternatingRowColor = Color.Beige;
     
    void radGridView1_ChildViewExpanded(object sender, ChildViewExpandedEventArgs e)
    {
        GridTableElement tableElement = (GridTableElement)this.radGridView1.GridViewElement.GetRowView(e.ChildRow.ViewInfo);
        tableElement.AlternatingRowColor = Color.Lavender;
    }

     
    Regards,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Edgar
    Edgar avatar
    2 posts
    Member since:
    Aug 2011

    Posted 26 Jun 2012 Link to this post

    hi team!!

    on my child template don't work!! :s

     rgvDetails.ChildViewExpanded += new ChildViewExpandedEventHandler(rgvDetails_ChildViewExpanded);
    
     rgvDetails.EnableAlternatingRowColor = true;
    
     rgvDetails.TableElement.AlternatingRowColor = Color.FromArgb(211,211,211);
    
    
    void rgvDetails_ChildViewExpanded(object sender, ChildViewExpandedEventArgs e)
    {
         GridTableElement tableElement = (GridTableElement)rgvDetails.GridViewElement.GetRowView(e.ChildRow.ViewInfo);
         tableElement.AlternatingRowColor = Color.FromArgb(211211211);
    }

    why?
  6. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 29 Jun 2012 Link to this post

    Hi Edgar,

    The ChildRow in this case is of type GridViewDetailsRowInfo and it lives inside the master GridTableElement. That is why when calling the GetRowView method, it returns the root table element. You can use the following code instead:
    void rgvDetails_ChildViewExpanded(object sender, ChildViewExpandedEventArgs e)
    {
        GridDetailViewRowElement row = radGridView1.TableElement.GetRowElement(e.ChildRow) as GridDetailViewRowElement;
        if (row != null)
        {
            row.ContentCell.ChildTableElement.AlternatingRowColor = Color.FromArgb(211, 211, 211);
        }
    }

    I hope it helps.
     
    Regards,
    Jack
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  7. Edgar
    Edgar avatar
    2 posts
    Member since:
    Aug 2011

    Posted 29 Jun 2012 Link to this post

    OMG! Thats correct!

    See you later for next question!

    Thanks!!

Back to Top