How to change row color in radgridview?

7 posts, 0 answers
  1. kharrat
    kharrat avatar
    1 posts
    Member since:
    Mar 2014

    Posted 31 Mar 2014 Link to this post

    for(int i =0; i<radGridView.rows.count;i++)
    {
                   if(RadGridview.Rows[i].Cells[0].value.ToString().Equlas("100")
                           {
                                     //change row color in radgridview
    }
    }
  2. Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 02 Apr 2014 Link to this post

    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.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Lester
    Lester avatar
    4 posts
    Member since:
    Jan 2011

    Posted 27 Jul 2014 Link to this post

    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
  5. Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 29 Jul 2014 Link to this post

    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.
     
  6. Lester
    Lester avatar
    4 posts
    Member since:
    Jan 2011

    Posted 29 Jul 2014 in reply to Ralitsa Link to this post

    Hi Ralitsa,

    Thanks for your help.

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

    thanks,

    Lester
  7. Alexander
    Alexander avatar
    3 posts
    Member since:
    Nov 2015

    Posted 19 Nov 2015 in reply to Ralitsa Link to this post

    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

  8. Ralitsa
    Admin
    Ralitsa avatar
    176 posts

    Posted 23 Nov 2015 Link to this post

    Hi Alexander,

    Thank you for contacting us. 

    I am not sure that I fully understand your case. In the attachments 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
Back to Top
UI for WinForms is Visual Studio 2017 Ready