)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add conditional formatting to rows?

Platform: WinForms |
Control: GridControl |
Published Date: May 13, 2016 |
Last Revised Date: May 29, 2019
Tags: formatting, row

In order to color a row if the value in column 2 is larger than 10 (or any logical condition that you can evaluate), the PrepareViewStyleInfo event can be used.

Code Snippet

C#

this.gridControl1.Model.Options.RefreshCurrentCellBehavior = GridRefreshCurrentCellBehavior.RefreshRow;
this.gridControl1.PrepareViewStyleInfo += gridControl1_PrepareViewStyleInfo;
 
 
void gridControl1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e)
{
    if (e.RowIndex > 0 && e.ColIndex > 0)
    {
        double d;
        string val = (e.ColIndex == 2) ? e.Style.Text : this.gridControl1[e.RowIndex, 2].Text;
        if (double.TryParse(val, System.Globalization.NumberStyles.Any, null, out d)
            && d > 10)
        {
             e.Style.BackColor = Color.LightGoldenrodYellow;
        }
    }
}

 

VB

Me.gridControl1.Model.Options.RefreshCurrentCellBehavior = GridRefreshCurrentCellBehavior.RefreshRow
AddHandler Me.gridControl1.PrepareViewStyleInfo, AddressOf gridControl1_PrepareViewStyleInfo
 
 
void gridControl1_PrepareViewStyleInfo(Object sender, GridPrepareViewStyleInfoEventArgs e)
   If e.RowIndex > 0 AndAlso e.ColIndex > 0 Then
   Dim d As Double
   Dim val As String = If((e.ColIndex = 2), e.Style.Text, Me.gridControl1(e.RowIndex, 2).Text)
   If Double.TryParse(val, System.Globalization.NumberStyles.Any, Nothing, d) AndAlso d > 10 Then
   e.Style.BackColor = Color.LightGoldenrodYellow
   End If
   End If

 

Screenshot

Showing backcolor by using conditional formatting to row in GridControl

Sample links:

C# FormattingRows_CS

VB FormattingRows_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon