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

RadGridView Row Formatting

9 Answers 1655 Views
GridView
This is a migrated thread and some comments may be shown as answers.
George Georgiou
Top achievements
Rank 1
George Georgiou asked on 14 Feb 2008, 09:55 AM
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


9 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 14 Feb 2008, 12:55 PM
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
0
Ron Coffee
Top achievements
Rank 1
answered on 24 Apr 2008, 02:58 PM
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 ;

}

0
Jack
Telerik team
answered on 24 Apr 2008, 03:32 PM
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
0
Tedd
Top achievements
Rank 1
answered on 14 Nov 2008, 09:18 PM
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
0
Nikolay
Telerik team
answered on 18 Nov 2008, 03:50 PM
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.
0
Juan Stoppa
Top achievements
Rank 1
answered on 07 Oct 2009, 10:25 AM
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
0
Robert
Top achievements
Rank 1
answered on 08 Oct 2009, 02:53 PM
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
0
gezim
Top achievements
Rank 1
answered on 10 Aug 2011, 11:57 AM
e.RowElement.BackColor = Color.     ????
I can't use this Color.anycolor 
0
Stefan
Telerik team
answered on 11 Aug 2011, 03:25 PM
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 >>

Tags
GridView
Asked by
George Georgiou
Top achievements
Rank 1
Answers by
Jack
Telerik team
Ron Coffee
Top achievements
Rank 1
Tedd
Top achievements
Rank 1
Nikolay
Telerik team
Juan Stoppa
Top achievements
Rank 1
Robert
Top achievements
Rank 1
gezim
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or