ComponentOne List for WinForms
In This Topic
    Owner-Drawn Cells
    In This Topic

    For cases where you need to perform complex per-cell customizations, you can draw directly to the list's device context by writing a handler for the OwnerDrawCell event. This event is fired as needed to display the contents of cells that have their OwnerDraw property set to True.

    The following steps implement the example depicted here:

    1. Bind C1List to the Composer DataSet. For more information, see Tutorial 1 - Binding C1List to a DataSet.
    2. Add the following handler for the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
          ' This line of code added by Visual Studio 2005.   
          Me.ComposerTableAdapter.Fill(Me.DsComposer.Composer)
       
          ' OwnerDraw the first column. 
          Me.C1List1.Splits(0).DisplayColumns(0).OwnerDraw = True
          Me.C1List1.Splits(0).DisplayColumns(0).Width = 2 * Me.C1List1.Splits(0).DisplayColumns(0).Width    
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Form1_Load(System.object sender, System.EventArgs e)     
      {
         
          // This line of code added by Visual Studio 2005.   
          this.ComposerTableAdapter.Fill(this.DsComposer.Composer);
      
          // OwnerDraw the first column. 
          this.c1List1.Splits[0].DisplayColumns[0].OwnerDraw = true;  
          this.c1List1.Splits[0].DisplayColumns[0].Width = 2 * this.c1List1.Splits[0].DisplayColumns[0].Width;  
      }
      

      The Form_Load event fills the list and creates the first column for use in the OwnerDrawCell event.

    3. Finally, implement the OwnerDrawCell event as follows:

      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  
          If e.Col = 0 Then
      
              ' Create a gradient brush, blue to red.
              Dim pt1, pt2 As Point
              Dim lineGrdBrush As System.Drawing.Drawing2D.LinearGradientBrush
              Dim ft As Font
              Dim br As Brush
              pt1 = New Point(e.CellRect.X, e.CellRect.Y)
              pt2 = New Point(e.CellRect.Right, e.CellRect.Y)
              lineGrdBrush = New System.Drawing.Drawing2D.LinearGradientBrush(pt1, pt2, Color.Blue, Color.Red)
      
              ' Fill the cell rectangle with the gradient.
              e.Graphics.FillRectangle(lineGrdBrush, e.CellRect)
      
              ' Draw string.
              ft = New Font("Arial", 8.25)
              br = New SolidBrush(Color.White)
              e.Graphics.DrawString(e.Text, ft, br, e.CellRect.X, e.CellRect.Y)
              lineGrdBrush.Dispose()
              ft.Dispose()
              br.Dispose()
      
              ' Let the list know that we handled the event.
              e.Handled = True
          End If    
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void C1List1_OwnerDrawCell( object sender, C1.Win.C1List.OwnerDrawCellEventArgs e)       
      {    
          if ( e.Col == 0 )    
          { 
              // Create a gradient brush, blue to red.
              pt1, pt2 Point;
             System.Drawing.Drawing2D.LinearGradientBrush lineGrdBrush;
              Font ft;
              Brush br;
              pt1 = new Point(e.CellRect.X, e.CellRect.Y);
              pt2 = new Point(e.CellRect.Right, e.CellRect.Y);
              lineGrdBrush = new System.Drawing.Drawing2D.LinearGradientBrush(pt1, pt2, Color.Blue, Color.Red);
      
              // Fill the cell rectangle with the gradient.
              e.Graphics.FillRectangle(lineGrdBrush, e.CellRect);
      
              // Draw string.
              ft = new Font("Arial", 8.25);
              br = new SolidBrush(Color.White);
              e.Graphics.DrawString(e.Text, ft, br, e.CellRect.X, e.CellRect.Y);
              lineGrdBrush.Dispose();
              ft.Dispose();
              br.Dispose();
      
              // Let the list know that we handled the event.
              e.Handled = true; 
           }     
      }
      
    Note: If you set the e.Handled argument to True, the list will not fill in the cell's background, nor will it display cell text or graphics. Therefore, you are responsible for filling in the entire cell, even if there is no data to display.