$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
DataGridView for C#.NET WinForms
Create custom data grid cells to GirdView in C# for your .NET Windows Forms applications
Home > WinForms UI Controls > User Manual > Conditional Formatting Grid Cells in C#

Format Grid Cells and Rows in C# Conditionally

The cells and rows in grid can be styled according to data conditions. The Kettic GridView provides the ConditionFormatObjects to accomplish the cells formatting according to data conditions. To enable the cell formatting functionality, we need to change the value of the ApplyToRow property to True for the cell objet that we are going to format. And there is a property, ApplySelectedRows, determining whether the cells formatting properties of the object will be applied to the selected rows.

Properties of Formatting Cells under Conditions

To create custom formatting cells under conditions, we need to use ConditionFormatObjects to accomplish that. The following are the properties to format cell, row, and text alignment in grid. This is achieved through applying ConditionFormatObjects which contains values like,
  • BackColorofCell, this property is used to customize the back color of gird cell
  • ForeColorofCell, this property is used to customize the fore color of gird cell
  • AlignTextToCell, this property is able to adjust the alignments of text in cell to GirdView
  • BackColorofRow, the property is used to customize the back color of the entire row where the cell placed,
  • ForeColorofCell, the property is used to customize the fore color of the entire row where the cell shows,
  • AlignTextToRow, the property is used to adjust alignments of text of the entire row where the cell in.

How to Format Cells in C# Conditonally

Rule based Formatting Objects

According to the conditions which define the formatting, the cell formatting objects can be divided to rule based and expression based. The rule based formatting objects will create custom formatting cells and rows to GirdView according to defined rules, including,
  • ConditionType, this property allows users to set conditions to Equal, Greater, Less, and Contains, etc.
  • TestValue1, this is a string used to stand for the first value testing the condition for formatting rows and cells
  • TestValue2, this is a string used to stand for the second value testing the condition for formatting rows and cells
We are going through an example as following to check whether the value in the second column ProductPrice is larger than 20, and if it exceeds the 20, set the back color to white, the text color to black, and the text alignment to MiddleLeft.

ConditionFormatObjects obj = new ConditionFormatObjects("MyCondition", ConditionTypes.Larger, "20", "", false);
obj.CellBackColor = Color.White;
obj.CellForeColor = Color.Black;
obj.TextAlignment = ContentAlignment.MiddleLeft;
this.ketticGridView1.Columns["ProductPrice"]. ConditionFormatObjectsList.Add(obj);

Expression based Formatting Objects

According to the conditions which define the formatting, the cell formatting objects can be divided to rule based and expression based. The expression based formatting objects will create custom formatting cells and rows to GirdView according to defined expression, which refers to data from different columns and result Boolean value. We are going through an example as following to check whether the value in the second column ProductPrice is larger than 20, and change the style of the ProductName column.

ExpressionFormatObject obj = new ExpressionFormatObject("MyCondition", "UnitPrice > 20", false);
obj.CellBackColor = Color.Green;
obj.CellForeColor = Color.Purple;
this.ketticGridView1.Columns["ProductName"].ConditionFormatObjectList.Add(obj);

Customize ConditionFormatForm in C#

The Kettic DataGridView allows end users easily create custom formatting object by using the form provided additionally. We can use the ConditionFormatFormDisplay event of Kettic DataGridView to access and customize the dialog. The following is an example to hide the invisible columns in the drop down and format the columns shown.

ExpressionFormatObject obj = new ExpressionFormatObject("MyCondition", "UnitPrice > 20", false);
obj.CellBackColor = Color.Green;
obj.CellForeColor = Color.Purple;
this.ketticGridView.Columns["ProductName"].ConditionFormatObjectList.Add(obj);
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls