ComponentOne List for WinForms
In This Topic
    Applying Cell Styles by Custom Criteria
    In This Topic

    For cases where regular expressions are insufficient to express your formatting requirements, you can use the FetchCellStyle event to customize fonts and colors on a per-cell basis. This event will only be fired for columns that have the FetchStyle property set to True.

    For example, you may want to provide color coding for values that fall within a certain range. The following code assumes that the FetchStyle property is True for a single column of numeric data, and handles the FetchCellStyle event to display values greater than 1000 in blue:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1List1_FetchCellStyle(ByVal sender As Object, ByVal e As C1.Win.C1List.FetchCellStyleEventArgs) Handles C1List1.FetchCellStyle        
        Dim N As Long        
        N = Val(Me.C1List1.Columns(e.Col).CellText(e.Row))
    
        If N > 1000 Then        
     e.CellStyle.ForeColor = System.Drawing.Color.Blue        
        End If       
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void C1List1_FetchCellStyle( object sender,  C1.Win.C1List.FetchCellStyleEventArgs e)        
    {        
        long N;        
        N = long.Parse(this.C1List1.Columns(e.Col).CellText(e.Row));
    
        if ( N > 1000 )        
        {        
     e.CellStyle.ForeColor = System.Drawing.Color.Blue;        
        }        
    }
    

    The Split, Row, and Col arguments identify which cell the list is displaying. The CellStyle argument conveys formatting information from your application to the list. Since the CellStyle argument is a Style object, you can also change a cell's font characteristics in the FetchCellStyle event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    If N > 1000 Then         
        e.CellStyle.Font = New Font(e.CellStyle.Font, FontStyle.Italic)        
    End If
    

    To write code in C#

    C#
    Copy Code
    if ( N > 1000 )        
    {        
        e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Italic);        
    }
    

    The FetchCellStyle event can also be used to apply formatting to one cell based upon the values of other cells, or even other controls. For example, suppose that you want to:

    In this case, you need to set the FetchStyle property to True for columns 4 and 7, and handle the FetchCellStyle event as follows:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1List1_FetchCellStyle(ByVal sender As Object, ByVal e As C1.Win.C1List.FetchCellStyleEventArgs) Handles C1List1.FetchCellStyle        
        Select Case e.Col        
     Case 4        
         Dim Col1 As Long, Col2 As Long        
         Col1 = CLng(Me.C1List1.Columns(1).CellText(e.Row))        
         Col2 = CLng(Me.C1List1.Columns(2).CellText(e.Row))        
         If Col1 - Col2 < 0 Then         
             e.CellStyle.ForeColor = System.Drawing.Color.Red        
         End If        
     Case 7       
         Dim S As String        
         S = Me.C1List1.Columns(7).CellText(e.Row)        
         If S = TextBox1.Text Then         
             e.CellStyle.Font = New Font(e.CellStyle.Font, FontStyle.Bold)        
         End If        
     Case Else       
         Debug.WriteLine ("FetchCellStyle not handled: " & e.Col)        
        End Select        
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void C1List1_FetchCellStyle( object sender, C1.Win.C1List.FetchCellStyleEventArgs e)        
    {        
        switch (e.Col)       
        {        
     case 4:        
         long Col1, Col2;        
         Col1 = long.Parse(this.C1List1.Columns[1].CellText(e.Row));        
         Col2 = long.Parse(this.C1List1.Columns[2].CellText(e.Row));       
         if ( Col1 - Col2 < 0 )        
         {        
             e.CellStyle.ForeColor = System.Drawing.Color.Red;        
         }        
         break;        
     case 7:        
         string  S;       
         S = this.C1List1.Columns[7].CellText(e.Row);        
         if ( S == TextBox1.Text )         
         {        
             e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);        
         }        
         break;        
     default:        
         Console.WriteLine ("FetchCellStyle not handled: " + e.Col);        
         break;        
        }        
    }
    

    For efficiency reasons, you should only set FetchStyle to True for columns that you plan to handle in the FetchCellStyle event.

    Note: The preceding examples use the CellText method for simplicity. However, the CellText and CellValue methods always create and destroy an internal clone of the dataset each time they are called, which may make them too inefficient to use in the FetchCellStyle event.

    If you need to customize fonts and colors on a per-row instead of a per-cell basis, use the FetchRowStyle event, which will only be fired once per row for lists that have the FetchRowStyles property set to True. The syntax for this event is as follows:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1List1_FetchRowStyle(ByVal sender As Object, ByVal e As C1.Win.C1List.FetchRowStyleEventArgs) Handles C1List1.FetchRowStyle
    

    To write code in C#

    C#
    Copy Code
    private void c1List1_FetchRowStyle(object sender, C1.Win.C1List.FetchRowStyleEventArgs e)
    

    Although you can use the FetchRowStyle event to implement an alternating row color scheme, an easier and more efficient way to accomplish the same task would be to use the AlternatingRows property, together with the built-in EvenRow and OddRow styles.