ComponentOne FlexGrid for WinForms
Conditional Formatting
Using the C1FlexGrid Control > Formatting Cells > Conditional Formatting

To format cells based on their contents, you can use the CellChanged event to select a style for the cell based on its contents. For example, the code below creates a new style for large currency values and applies it to cells based on their contents:

To write code in Visual Basic

Visual Basic
Copy Code
Dim cs As C1.Win.C1FlexGrid.CellStyle
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    ' Create a custom style for large values.
    cs = _flex.Styles.Add("LargeValue")
    cs.Font = New Font(Font, FontStyle.Italic)
    cs.BackColor = Color.Gold
End Sub
 
 ' Format cells based on their content.
Private Sub _flex_CellChanged(ByVal sender As Object, ByVal e As RowColEventArgs) Handles _flex.CellChanged
 
     ' Mark currency values > 50,000 as LargeValues (reset others by setting their Style to Nothing).
    Dim cs As CellStyle
    If _flex(e.Row, e.Col).ToString >= 50000 Then
        cs = _flex.Styles("LargeValue")
        _flex.SetCellStyle(e.Row, e.Col, cs)
    End If
End Sub

To write code in C#

C#
Copy Code
CellStyle cs;
 
private void Form1_Load(object sender, EventArgs e)
{
 
     // Create a custom style for large values.
    cs = _flex.Styles.Add("LargeValue");
    cs.Font = new Font(Font, FontStyle.Italic);
    cs.BackColor = Color.Gold;
}
 
 // Format cells based on their content.
private void _flex_CellChanged( object sender, RowColEventArgs e) 
{
     // Mark currency values > 50,000 as LargeValues reset others by setting their Style to null).
    
    if (Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(_flex[e.Row, e.Col].ToString()) >= 50000)
    {
        cs = _flex.Styles["LargeValue"];
        _flex.SetCellStyle(e.Row, e.Col, cs);
    }
}
See Also