![]() |
![]() |
Basics - Working With Style Conditions |
The StyleConditions collection gives you a flexible and powerful way to apply style to an object that meets specified condition. Style conditions can be applied to cells and rows. There are three places where you can define style conditions:
The code bellow demonstrates style condition usage. Note that last parameter in the RowStyleConditions.Add method is a key of the column whose values are used when evaluating condition. That parameter is ignored when adding style conditions to the column's style condition collection.
Private Sub SGGrid1_OnInit()
Dim i As Integer, j As Integer
With SGGrid1
'create columns and rows
.Columns.RemoveAll False
For i = 1 To 5
With .Columns.Add("Column " & Trim(i))
.DataType = sgtShort
End With
Next
.DataRowCount = 10
'create new global cell style condition
With .Styles.Add("CellCond")
.BackColor = vbRed
.BackColor2 = vbWhite
.BkgStyle = sgCellBkgGradientVertical
End With
.CellStyleConditions.Add _
"CellCond", sgConditionCellValue, _
sgOpBetween, 2, 12
'create new row style condition
With .Styles.Add("RowCond")
.Font.Bold = True
.ForeColor = RGB(0, 128, 0)
.BackColor = vbYellow
.TextAlignment = sgAlignCenterCenter
End With
.RowStyleConditions.Add _
"RowCond", sgConditionCellValue, _
sgOpBetween, 2, 12, "Column 3"
'create new column style condition
With .Styles.Add("ColCond")
Set .BkgPicture = Me.Icon
.BkgPictureAlignment = sgPicAlignTile
End With
.Columns("Column 4").StyleConditions.Add _
"ColCond", sgConditionCellValue, sgOpGreaterThan, 5
End With
'populate grid with data
For i = 0 To 9
For j = 0 To 4
SGGrid1.Array.Value(i, j) = i * j
Next
Next
End Sub
The column's DataType property is important and determines how are conditions evaluated. For example, when column type is sgtString, value "400" is smaller than value "5".
The code below shows results for a different data types.
Private Sub SGGrid1_OnInit()
SGGrid1.Columns.RemoveAll False
SGGrid1.DataRowCount = 5
With SGGrid1.Columns.Add("String")
.DataType = sgtString
End With
With SGGrid1.Columns.Add("Number")
.DataType = sgtDouble
End With
With SGGrid1.Columns.Add("Date")
.DataType = sgtDateTime
End With
SGGrid1.CellStyleConditions.Add "Heading", sgConditionCellValue, sgOpGreaterThan, 400
With SGGrid1.Array
'string column
.Value(0, 0) = "39898" 'No
.Value(1, 0) = "5" 'Yes
'number column
.Value(0, 1) = 39898 'Yes
.Value(1, 1) = 5 'No
'date column
.Value(0, 2) = CDate(39898) 'Yes
.Value(1, 2) = CDate(5) 'No
End With
End Sub