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

Loop rows and change background color

8 Answers 1025 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Danilo
Top achievements
Rank 1
Danilo asked on 18 Mar 2013, 08:23 AM
Hello, I'm loonking for a way to change the background color of some rows  in a GridView after databinding.
Currently I use the loadComplete Event to loop the rows:
RadGridView table = sender as RadGridView;
        
        foreach (GridViewRowInfo r in table.Rows)
       {
 
       }
Unfortunately I was not able to find a way to change the background color of the GridVieRowInfo Elements.
It is important that I can change the backround by looping because the rows which should get colored are not periodical.
Can someone tell me how to do that?
Any help very apreciated. Thank you

8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Mar 2013, 09:03 AM
Hi,

You can customize the rows in RowFormatting event as shown below.
C#:
protected void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if (e.RowElement.RowInfo.Cells["Columnname"].Value == "text")
    {
        e.RowElement.DrawFill = true;
        e.RowElement.GradientStyle = GradientStyles.Solid;
        e.RowElement.BackColor = Color.Red;
    }
}

Thanks,
Princy
0
Stefan
Telerik team
answered on 20 Mar 2013, 01:43 PM
Hello guys,

I can confirm that Princy's suggestion is the right way to go in this case. 

@Princy - your 
Telerik Points have been updated for the community effort.

Regards,
Stefan
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
0
Mujtaba Ali
Top achievements
Rank 1
answered on 12 Jun 2013, 10:13 PM
Hi

I want to change the row's background color outside of RowFormatting event, is it possible?

Regards,

Mujtaba Ali Panjwani
0
Stefan
Telerik team
answered on 17 Jun 2013, 10:50 AM
Hello Mujtaba Ali,

Thank you for writing.

The correct place to change the Backcolor of a row is the RowFormatting event of RadGridView. Could you please provide me with information why you do not want to use this event?

Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
0
Mujtaba Ali
Top achievements
Rank 1
answered on 17 Jun 2013, 12:49 PM
Hi

Thanks for the reply.

I wanted to change the row's background color when a row is right clicked. It would be the best to change the backcolor directly but currently I have to store row index and then call the update method to trigger the formatting event which, of course, doesn't look like a good idea.

Regards,

Mujtaba Ali Panjwani
0
Stefan
Telerik team
answered on 20 Jun 2013, 10:58 AM
Hello Mujtaba Ali,

To do that you can use the CellClick event of the grid, where you can get the clicked cell and its row. Set the row Tag to something and use this Tag in the formatting event to identify the rows you need to style. Here is an example:
void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
{
    e.Row.Tag = "something";
}
 
void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    if (e.RowElement.RowInfo.Tag != null)
    {
        e.RowElement.BackColor = Color.Red;
        e.RowElement.GradientStyle = GradientStyles.Solid;
        e.RowElement.DrawFill = true;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
    }
}

I hope this helps.
 
Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
0
Mujtaba Ali
Top achievements
Rank 1
answered on 20 Jun 2013, 01:42 PM
Hi

Thanks for the reply. They are very helpful.

I have done it approximately the same style but I also had to call
grdPrvw.TableElement.Update(GridUINotifyAction.StateChanged)

But I still think that there could be better ways to change the appearance instead of using only RowFormatting event.

Also I had to do some workaround to get if right button was clicked or left using Grid's "MouseClick" event.

Does the RadGrid have any generic way to detect this within CellClick event.

Thanks for your time.

Regards,

Mujtaba Ali Panjwani
0
Stefan
Telerik team
answered on 25 Jun 2013, 08:06 AM
Hello Danilo,

Thank you for writing back.

1. The RowFormatting event is the right place to introduce appearance modification for the rows in the grid as the control uses UI virtualization for its cells. More information is available here: Grid Explained: RadGridView for WinForms virtualization and its implications. The case with CellElement and RowElement.

2. You can move the logic from the CellClick to the MouseDown event. Here is how it can look like:
void radGridView1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        GridDataCellElement dataCell = radGridView1.ElementTree.GetElementAtPoint(e.Location);
 
        if (dataCell != null)
        {
            dataCell.RowInfo.Tag = "something";
        }
    }
}

I hope this helps.
 
Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
Tags
GridView
Asked by
Danilo
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Stefan
Telerik team
Mujtaba Ali
Top achievements
Rank 1
Share this question
or