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. Image for the cookie policy date
close icon

GridGroupingControl and row background color

How do I set background color on a row depending on a condition?
The help documentation says that I should create a GridConditionalFormatDescriptor class, set the Expression property of it, and then the color value of the Appearance.RecordFieldCell property, and conclude by calling TableDescriptor.ConditionalFormats.Add() with the GridConditionalFormatDescriptor object as parameter.

As far as I can tell from the documentation, this only applies to Windows Forms. How should I accomplish this in ASP.NET? I use Visual Studio 2005 with SyncFusion 5.1.1.0. The ASP.NET uses version 2 of dotnet.

15 Replies

GB Gokul B Syncfusion Team July 12, 2007 10:11 PM UTC

Hi Thomas,

As mentioned in our help documentation, you have to use the GridConditionalFormatDescriptor class to apply the cell style. Attached is the sample which Illustrates how different formats can be applied to the grid’s cells, conditionally, based on the cell’s values.

http://websamples.syncfusion.com/samples/Grid.Web/5.1.0.51/F63992/GridGroupingControl_forum63992/main.htm

* GridConditionalFormatDescriptor is used to set Styles for grid cells according to the conditions specified.
* Adding the above descriptor to the corresponding TableDescriptor will affect all the cells in that table.

Let us know if it helps.

Thanks for using Syncfusion products.

Regards,
Gokulkumar B


YY Yarius Yacinus July 19, 2007 01:04 PM UTC

Hi Gollukurman,

In you sample your compare the selected item of DDL and the value in your grid,but I would like to know how to compare directely the value of the column record (speciely I have problem with date comparaison beetween the value in the grid which is DateTime type and current date DateTime.today) and how to set backcolor only for this column not in anyrecordfieldcell.
below part of my code (sorry I couldn't make as an attachement due to busy work)
protected void set_color(){
System.Drawing.Color C = new Color();
C=Color.FromName("red");
string d=System.Convert.ToString(DateTime.Now);
Response.Write(DateTime.Parse("18/07/2007").ToString());
GridConditionalFormatDescriptor gcfd1 = new GridConditionalFormatDescriptor();
gcfd1.Name = "Criteria1";
string str=GridGroupingControl1.TableDescriptor.Columns[15].ToString();
gcfd1.Expression=System.Convert.ToString(str.CompareTo("18/07/2007"));
gcfd1.Appearance.RecordFieldCell.BackColor=C;
this.GridGroupingControl1.TableDescriptor.ConditionalFormats.Add(gcfd1);


thank you
Ragards

Yacinus


VA Valli Syncfusion Team July 20, 2007 06:58 AM UTC

Hi Yacinus,

Attached is a sample as per your requirement.

http://websamples.syncfusion.com/samples/Grid.Web/5.1.0.51/Forum-63992/main.htm

The above sample compares 2 date values. It compares the datevalue of the 'HireDate' column with the date value '5/1/1992'. If you need you can replace '5/1/1992' with current date.

Based on the result the backcolor of the cell is changed.

1. When the hiredate is equal to '5/1/1992' the backcolor of the cell is changed to Green.
2. When the hiredate is greater than '5/1/1992' the backcolor of the cell is changed to yellow.
3. When the hiredate is less than than '5/1/1992' the backcolor of the cell is changed to red.

All the described functionalities are done in the 'OnQueryCellStyleInfo' event.

Please try running the sample and let us know if this helps you.

Regards,
Valli


RI Ricardo Ismael Sanchez Condori February 26, 2010 10:05 PM UTC

hi, the link is break ! does anyone write the code in a new post!.

greetings !


RC Rajadurai C Syncfusion Team February 27, 2010 02:59 AM UTC

Hi Ismael,

Thanks for your interest in Syncfusion Products.

I apologize for the inconvenience caused with the broken link. Please make use of the following modified link for the same.
http://help.syncfusion.com/samples/Grid.Web/5.1.0.51/Forum-63992/main.htm

Regards,
Rajadurai


FC Falcon CK September 11, 2016 08:46 AM UTC

Hi, can i have the exactly same function but is windows form ?


AR Amal Raj U Syncfusion Team September 12, 2016 06:23 AM UTC

Hi Falcon, 
 
Thanks for using Syncfusion products. 
 
We have created a simple sample based on your requirement in Conditional Formatting to set the Back Color of a row based on condition. Please make use of the below sample and the code, 
 
Code Example 
//Initialize Condition Format. 
GridConditionalFormatDescriptor gcfd = new GridConditionalFormatDescriptor(); 
gcfd.Appearance.AnyRecordFieldCell.BackColor = Color.Green; 
gcfd.Appearance.AnyRecordFieldCell.TextColor = Color.White; 
var checkDate = DateTime.Now.AddDays(1).ToString("MM/dd/yyyy"); 
gcfd.Expression= "[HiredDate] < \'"+checkDate+"\'"; 
 
//To add the conditional format instances to the ConditionalFormats collection. 
this.gridGroupingControl1.TableDescriptor.ConditionalFormats.Add(gcfd); 
 
Sample Link 
 
For more information about Conditional Formatting, please refer to the below User guide documentation and our Dashboard sample, 
 
UG Link 
 
Dashboard Sample Location 
<Install_Location>\Syncfusion\EssentialStudio[Version_Number]\Windows\Grid.Grouping.Windows\Samples\Styling and Formatting\Conditional Formatting Demo 
 
Regards, 
Amal Raj U. 



FC Falcon CK September 12, 2016 09:27 AM UTC

Hi, thank you for reply, but i think you've misunderstand my question. 
I need to make a styling for a particular cell, based on another column value. 
For eg.

Row 1
Cell['gender'] => "male"
Cell['name'] => styling as blue


Row 2
Cell['gender'] => "female"
Cell['name'] => styling as pink

The styling is based on that particular row and certain column's value. Is that possible to do it in windows form and i am using grid grouping control. 


AR Amal Raj U Syncfusion Team September 13, 2016 04:44 AM UTC

Hi Falcon, 

Thanks for the update. 

The reported scenario can be achieved by using QueryCellStyleInfo event. Please make use of the below code and the sample, 
 
Code Example 
//Event Subscription. 
this.gridGroupingControl1.QueryCellStyleInfo += new GridTableCellStyleInfoEventHandler(gridGroupingControl1_QueryCellStyleInfo); 
 
void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e) 
    GridTableCellStyleInfo style = e.Style as GridTableCellStyleInfo
    if (style != null && style.TableCellIdentity != null && style.TableCellIdentity.DisplayElement != null 
        && style.TableCellIdentity.DisplayElement.GetRecord() != null && style.TableCellIdentity.Column != null
    { 
        Record record = style.TableCellIdentity.DisplayElement.GetRecord(); 
        string value = record["Gender"].ToString(); 
        if (value == "Male" && style.TableCellIdentity.Column.Name == "Name"
        { 
            style.BackColor = Color.Blue; 
            style.TextColor = Color.White; 
        } 
        else if (style.TableCellIdentity.Column.Name == "Name"
        { 
            style.BackColor = Color.Pink; 
            style.TextColor = Color.White; 
        } 
    } 
 
Sample Link 
 
Regards, 
Amal Raj U. 



FC Falcon CK September 13, 2016 02:33 PM UTC

Hi, awesome, thank you, it works !!


AR Amal Raj U Syncfusion Team September 14, 2016 04:42 AM UTC

Hi Falcon, 

Thanks for the update. 

We are glad to know that the issue has been resolved at your end, please let us know if you have any other concerns. 

Regards, 
Amal Raj U.          



DA Dash December 8, 2016 02:58 AM UTC

Hello.  I added one line to the sample project:


gridGroupingControl1.TableOptions.ListBoxSelectionMode = SelectionMode.MultiExtended;

With that option, the row selection color overrides the blue/pink color when a row and some column other than Name is selected.  Is there any way to exempt certain columns from the row selection color (in this case "Name") so the color scheme stays with the column regardless of whether the row is selected?

Thank you.




AR Amal Raj U Syncfusion Team December 8, 2016 11:17 AM UTC

Hi Vincent, 

Thanks for using Syncfusion products. 

By default, when Selection mode is enabled, the default SelectionBackColor will override the cells BackColor for selection. If you want to avoid this overriding of SelectionBackColor, then the TableControlDrawCellDisplayText event can be used. Please make use of the below code, 
 
Code Example 
//Event Subscription. 
this.gridGroupingControl1.TableControlDrawCellDisplayText += new GridTableControlDrawCellDisplayTextEventHandler(gridGroupingControl1_TableControlDrawCellDisplayText); 
 
void gridGroupingControl1_TableControlDrawCellDisplayText(object sender, GridTableControlDrawCellDisplayTextEventArgs e) 
    GridTableCellStyleInfo style = e.Inner.Style as GridTableCellStyleInfo
    if (style != null && style.TableCellIdentity != null && style.TableCellIdentity.DisplayElement != null 
        && style.TableCellIdentity.DisplayElement.GetRecord() != null && style.TableCellIdentity.Column != null
    { 
        Record record = style.TableCellIdentity.DisplayElement.GetRecord(); 
        string value = record["Gender"].ToString(); 
        if (record.IsSelected()) 
        { 
            if (value == "Male" && style.TableCellIdentity.Column.Name == "Name"
            { 
                e.Inner.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Inner.TextRectangle); 
            } 
            else if (style.TableCellIdentity.Column.Name == "Name"
            { 
                e.Inner.Graphics.FillRectangle(new SolidBrush(Color.Pink), e.Inner.TextRectangle); 
            } 
        } 
    } 
 
Sample Link 
 
 
 
Regards, 
Amal Raj U. 



DA Dash December 8, 2016 12:16 PM UTC

Thank you, Amal.  Works perfectly!


AR Amal Raj U Syncfusion Team December 8, 2016 02:05 PM UTC

Hi Vincent, 

Thanks for your update. 

We are glad to that the provided solution in our last update has resolved your query. Please let us know, if you have any other concerns. 

Regards, 
Amal Raj U. 


Loader.
Live Chat Icon For mobile
Up arrow icon