ComponentOne List for WinForms
In This Topic
    Tutorial 15 - OwnerDraw List Cells
    In This Topic

    In this tutorial you will learn to format your list with the OwnerDrawCell event.

    1. Follow steps 1 through 6 of Tutorial 8 - Displaying Translated Data, setting the SQL statement to SELECT * FROM CUSTOMER, to create a project with a C1List bound to a Data Set.
    2. Define the following variables at the Form level:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       Dim bh1, bh2 As Brush
       Dim ft As Font
      

      To write code in C#

      C#
      Copy Code
       bh1, bh2 Brush;
       ft Font;
      
    3. In the Form_Load event, add the following code:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       ' Fill the data.      
       Me.CustomerTableAdapter.Fill(Me.DsCustomer.Customer)
      
       ' Owner draw every column.      
       Dim i As Integer     
       For i = 0 To Me.C1List1.Splits(0).DisplayColumns.Count - 1     
           Me.C1List1.Splits(0).DisplayColumns(i).OwnerDraw = True     
       Next i
      
       ' Set the row height.      
       Me.C1List1.ItemHeight = 25
      
       ' Set the brushes and font.      
       bh1 = New SolidBrush(Color.Pink)
       bh2 = New SolidBrush(Color.Yellow)     
       Dim ff As FontFamily     
       ff = New FontFamily("Arial")     
       ft = New Font(ff, 14, FontStyle.Regular, GraphicsUnit.Pixel)
      

      To write code in C#

      C#
      Copy Code
       // Fill the data.      
       this.CustomerTableAdapter.Fill(this.DsCustomer.Customer); 
      
       // Owner draw every column.      
       int i;     
       for ( i = 0; i <= this.c1List1.Splits[1].DisplayColumns.Count - 1; i++)     
       {     
           this.c1List1.Splits[0].DisplayColumns[i] OwnerDraw = true;     
       }
        
       // Set the row height.      
       this.c1List1.ItemHeight = 25;
      
       // Set the brushes and font.      
       bh1 = new SolidBrush(Color.Pink);     
       bh2 = new SolidBrush(Color.Yellow);     
       FontFamily ff;     
       ff = new FontFamily("Arial");     
       ft = new Font(ff, 14, FontStyle.Regular, GraphicsUnit.Pixel);
      
    4. Add the following code to the OwnerDrawCell event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       Private Sub C1List1_OwnerDrawCell(ByVal sender As Object, ByVal e As C1.Win.C1List.OwnerDrawCellEventArgs) Handles C1List1.OwnerDrawCell
      
           ' Draw alternative background.      
           If (e.Row Mod 2 = 0) Then     
               e.Graphics.FillRectangle(Me.bh1, e.CellRect)     
           Else     
               e.Graphics.FillRectangle(Me.bh2, e.CellRect)     
           End If     
           e.Graphics.DrawString(e.Text, Me.ft, New SolidBrush(Color.Black), e.CellRect.Left, e.CellRect.Top)     
           e.Handled = True     
       End Sub
      

      To write code in C#

      C#
      Copy Code
       private void C1List1_OwnerDrawCell(object sender, C1.Win.C1List.OwnerDrawCellEventArgs e)     
       {    
           // Draw alternative background.    
           if ((e.Row % 2 == 0) )    
           {    
               e.Graphics.FillRectangle(this.bh1, e.CellRect);    
           }    
           else    
           {    
               e.Graphics.FillRectangle(this.bh2, e.CellRect);    
           }     
           e.Graphics.DrawString(e.Text, this.ft, new SolidBrush(Color.Black), e.CellRect.Left, e.CellRect.Top);     
           e.Handled = true;     
       }
      

    Run the program and observe the following:

    C1List1 displays the data using the font and color changes specified in step 4.

    This concludes the tutorial.