GrapeCity MultiRow Windows Forms Documentation
Changing Cell Styles based on Conditions

You can apply styles to cells when the cells satisfy a set of pre-defined conditions, with conditional cell styles (ConditionalCellStyle class).

List of Conditions

The built-in conditions, which you can specify in the ConditionalCellStyleItem class, are as follows:

ConditionalCellStyleOperator Value 1 Value 2 Description
IsNull - - Cell value is null reference (Nothing in Visual Basic) or DBNull.Value.
IsNotNull - - Cell value is not null reference (Nothing in Visual Basic) or DBNull.Value.
IsEmpty - - Cell value is null reference (Nothing in Visual Basic), DBNull.Value, or String.Empty.
IsNotEmpty - - Cell value is not empty (Nothing in Visual Basic), DBNull.Value, or String.Empty.
IsTrue - - Cell value is True.
IsFalse - - Cell value is False.
Between Valid Valid Cell value is included in the range defined by Value 1 and Value 2.
NotBetween Valid Valid Cell value is not included in the range defined by Value 1 and Value 2.
Equals Valid - Cell value is the same as that of Value 1.
NotEquals Valid - Cell value is not the same as that of Value 1.
GreaterThan Valid - Cell value is greater than Value 1.
LessThan Valid - Cell value is smaller than Value 1.
GreaterThanOrEquals Valid - Cell value is greater than or equal to Value 1.
LessThanOrEquals Valid - Cell value is smaller than or equal to Value 1.
Contains Valid - Cell value contains Value 1 (String comparison).
StartsWith Valid - Cell value starts with Value 1 (String comparison).
EndsWith Valid - Cell value ends with Value 1 (String comparison).
IsMatch Valid - Cell value matches the Regular Expression of Value 1.
IsNotMatch Valid - Cell value does not match the Regular Expression of Value 1.

Developers can use dynamic cell styles in order to define independent conditions. For details, refer to User Defined Conditional Styles.

Using Code

The following code sets the cell color to red when the cell value is null (Nothing in Visual Basic) and blue when it is not null.

[VB]

Imports GrapeCity.Win.MultiRow

Dim conditionalCellStyle1 As New ConditionalCellStyle()

' Style when cell value is  null
Dim cellStyle1 As New CellStyle()
cellStyle1.BackColor = Color.FromArgb(255, 192, 192)
Dim item1 As New ConditionalCellStyleItem(cellStyle1, ConditionalCellStyleOperator.IsNull)
conditionalCellStyle1.Items.Add(item1)

' Style when the cell value is not null
Dim cellStyle2 As New CellStyle
cellStyle2.BackColor = Color.FromArgb(192, 192, 255)
Dim item2 As New ConditionalCellStyleItem(cellStyle2, ConditionalCellStyleOperator.IsNotNull)
conditionalCellStyle1.Items.Add(item2)

[CS]

using GrapeCity.Win.MultiRow;

ConditionalCellStyle conditionalCellStyle1 = new ConditionalCellStyle();

// Style when cell value is  null
CellStyle cellStyle1 = new CellStyle();
cellStyle1.BackColor = Color.FromArgb(255, 192, 192);
ConditionalCellStyleItem item1 = new ConditionalCellStyleItem(cellStyle1, ConditionalCellStyleOperator.IsNull);
conditionalCellStyle1.Items.Add(item1);

// Style when the cell value is not null
CellStyle cellStyle2 = new CellStyle();
cellStyle2.BackColor = Color.FromArgb(192, 192, 255);
ConditionalCellStyleItem item2 = new ConditionalCellStyleItem(cellStyle2, ConditionalCellStyleOperator.IsNotNull);
conditionalCellStyle1.Items.Add(item2);

The following code applies the above conditional cell style to the cells.

[VB]

Dim template1 As Template = Template.Default
template1.Row.Cells(1).Style = conditionalCellStyle1
GcMultiRow1.Template = template1
GcMultiRow1.RowCount = 3

[CS]

Template template1 = Template.Default;
template1.Row.Cells[1].Style = conditionalCellStyle1;
gcMultiRow1.Template = template1;
gcMultiRow1.RowCount = 3;

Since the cell does not have a value in the default state, the backcolor of the cell is set to red. When a value is entered in the cell, the backcolor of the cell becomes blue.

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options