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
9 Answers, 1 is accepted
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
if
(e.RowElement.IsOdd)
{
e.RowElement.DrawFill =
true;
e.RowElement.BackColor =
Color.LightGray;
}
else
{
e.RowElement.DrawFill =
true;
e.RowElement.BackColor =
Color.White ;
}
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
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
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.
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
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; |
} |
} |
} |
I can't use this Color.anycolor
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 >>