How to change row color in radgridview?

1 Answer 6515 Views
CAB Enabling Kit
kharrat
Top achievements
Rank 1
kharrat asked on 31 Mar 2014, 06:48 PM
for(int i =0; i<radGridView.rows.count;i++)
{
               if(RadGridview.Rows[i].Cells[0].value.ToString().Equlas("100")
                       {
                                 //change row color in radgridview
}
}

1 Answer, 1 is accepted

Sort by
1
Ralitsa
Telerik team
answered on 02 Apr 2014, 08:39 AM
Hi Kharrat, 

Thank you for contacting us. 

I suppose that you want to change the BackColor property of a row in RadGridView. If this is what you want to achieve, you can subscribe to the RowFormatting event and change the color. Please take a look at the following code example: 

a) subscribe to the RowFormatting event
this.radGridView1.RowFormatting += radGridView1_RowFormatting;

b) change the color 
void radGridView1_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
{
    if ((int)e.RowElement.RowInfo.Cells["MyCount"].Value == 100)
    {
        e.RowElement.DrawFill = true;
        e.RowElement.GradientStyle = GradientStyles.Solid;
        e.RowElement.BackColor = Color.GreenYellow;
    }
    else
    {
        e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
    }
}

In order to prevent applying the formatting to other columns' cell elements all customization should be reset for the rest of the cell elements when use events like ViewCellFormatting, ViewRowFormatting, CellFormatting, RowFormatting. 

In our documentation there are articles which demonstrate how you can customize your RadGridView by using the formatting events. You can refer to articles Formatting Rows and Formatting Cells. 

I hope this will help. Do not hesitate to write back with further questions.
 
Regards,
Ralitsa
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Lester
Top achievements
Rank 1
commented on 28 Jul 2014, 02:19 AM

Hi Ralitsa,

I need to do the same but in VB.net. Used the example you showed as in the documentation but I am getting the following error:

"NullReferenceException"

Can you help me out please?

This is my code:

Private Sub grdSrchResult_RowFormatting(sender As System.Object, e As Telerik.WinControls.UI.RowFormattingEventArgs) Handles grdSrchResult.RowFormatting

        If e.RowElement.RowInfo.Cells("STATUS").Value = "Created" Or e.RowElement.RowInfo.Cells("STATUS").Value = "In Production" Then
            e.RowElement.DrawFill = True
            e.RowElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid
            e.RowElement.BackColor = Color.Red
        Else
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local)
        End If

    End Sub
Ralitsa
Telerik team
commented on 29 Jul 2014, 11:13 AM

Hi Lester,

Thank you for contacting us. 

I was not able to reproduce the issue using your code. I have attached my sample project used for my tests. Can you please take a look at it and let me know how it differs from your setup? In addition please specify which version of Telerik UI for Winforms you use. Thank you in advance. 

I am looking forward to your reply.

Regards,
Ralitsa
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Lester
Top achievements
Rank 1
commented on 29 Jul 2014, 05:49 PM

Hi Ralitsa,

Thanks for your help.

I got it to work and the issue was not related to the RowFormatting.

thanks,

Lester
Alexander
Top achievements
Rank 1
commented on 19 Nov 2015, 07:20 AM

Hello.
In my scenario, I don't want to change the default style or a default color of the selected row. Thus it seems to me not right to use Radgridview_RowFormating handler. But in certain conditions, I would like to change the selection color of the specific row.
For example from the SelectionChanged event handler of RadGridView, if a certain condition occurs, I would like to highlight a specific row with a different color.
The problem is that there is no such property like:
myGrid.SelectedItem.Background = Brushes.Red  (and I want this to happen only for this specific time, and not always) or

myGrid.Rows[i].Background = Brushes.Red

How can I handle this scenario?

I hope you can help me,
Thanks in advance

Ralitsa
Telerik team
commented on 23 Nov 2015, 07:57 AM

Hi Alexander,

Thank you for contacting us. 

I am not sure that I fully understand your case. In the you can find sample code and video demonstrating how to change the BackColor of a specific selected row using RowFormatting event and how to use ConditionalFormatting to change the appearance of a row when selecting another row. 

If this is not what you want to achieve, I would kindly ask you to send me a more detailed explanation what is your case. 

Let me know if I can assist you further.

Regards,
Ralitsa
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
CAB Enabling Kit
Asked by
kharrat
Top achievements
Rank 1
Answers by
Ralitsa
Telerik team
Share this question
or