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

Changing the AlternatingRowColor of a Gridview Programmatically

6 Answers 164 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremy Murtishaw
Top achievements
Rank 1
Jeremy Murtishaw asked on 22 May 2009, 07:37 PM
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

6 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 25 May 2009, 08:47 AM
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.
0
Sheraz Naseeb
Top achievements
Rank 1
answered on 01 Sep 2010, 02:54 PM
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
0
Jack
Telerik team
answered on 02 Sep 2010, 04:28 PM
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
0
Edgar
Top achievements
Rank 1
answered on 26 Jun 2012, 10:46 PM
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?
0
Jack
Telerik team
answered on 29 Jun 2012, 01:07 PM
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 >>
0
Edgar
Top achievements
Rank 1
answered on 29 Jun 2012, 05:35 PM
OMG! Thats correct!

See you later for next question!

Thanks!!

Tags
GridView
Asked by
Jeremy Murtishaw
Top achievements
Rank 1
Answers by
Jack
Telerik team
Sheraz Naseeb
Top achievements
Rank 1
Edgar
Top achievements
Rank 1
Share this question
or