Reports for WinForms | ComponentOne
Getting Started with Reports for WinForms / Getting Started with Printing and Previewing / Creating Tables / Creating a Table with Three Columns and Rows / Creating a Background Color for Specific Cells in the Table
In This Topic
    Creating a Background Color for Specific Cells in the Table
    In This Topic

    This topic demonstrates how to create background colors for specific cells in the table. It also demonstrates how to use the TableCell.CellStyle property to set the styles used in the table that will be rendered. This topic assumes you have a table with three columns and three rows.

    1. The following code below should already exist in your source file:

      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
      
          ' Make a table.    
          Dim table As C1.C1Preview.RenderTable = New C1.C1Preview.RenderTable(Me.C1PrintDocument1)    
          table.Style.GridLines.All = New C1.C1Preview.LineDef(Color.DarkGray)
      
          Dim r As Integer = 3    
          Dim c As Integer = 3    
          Dim row As Integer    
          Dim col As Integer    
          For row = 0 To r - 1 Step +1    
              For col = 0 To c - 1 Step +1    
                  Dim celltext As C1.C1Preview.RenderText = New C1.C1Preview.RenderText(Me.C1PrintDocument1)
      
                  ' Add empty cells.     
                  celltext.Text = String.Format("", row, col)    
                  table.Cells(row, col).RenderObject = celltext    
              Next    
          Next
                                      
          ' Generate the document.     
          Me.C1PrintDocument1.Body.Children.Add(table)    
          Me.C1PrintDocument1.Generate()    
      End Sub
      

      To write code in C#

      C#
      Copy Code
      private void Form1_Load(object sender, System.EventArgs e)    
      {   
          // Make a table.     
          C1.C1Preview.RenderTable table = new C1.C1Preview.RenderTable(this.c1PrintDocument1);    
          table.Style.GridLines.All = new C1.C1Preview.LineDef(Color.DarkGray);
      
          const int r = 3;    
          const int c = 3;    
          for (int row = 0; row < r; ++row)    
          {    
              for (int col = 0; col < c; ++col)    
              {    
                  C1.C1Preview.RenderText celltext = new C1.C1Preview.RenderText(this.c1PrintDocument1);    
                  celltext.Text = string.Format("", row, col);
      
                  // Add empty cells.     
                  table.Cells[row, col].RenderObject = celltext;    
              }    
          }
      
          // Generate the document.     
          this.c1PrintDocument1.Body.Children.Add(table);    
          this.c1PrintDocument1.Generate();    
      }
      
    2. Make the table's width and height fifteen centimeters long:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      table.Height = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm)    
      table.Width = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm)
      

      To write code in C#

      C#
      Copy Code
      table.Height = new C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm);
      table.Width = new C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm);
      
    3. Add the following code below the last line of code listed above. This code will create a crimson background color for row 1, column 2.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      table.Cells(1, 2).CellStyle.BackColor = Color.Crimson
      

      To write code in C#

      C#
      Copy Code
      table.Cells[1, 2].CellStyle.BackColor = Color.Crimson;
      
      Note: The rows and columns begin with 0. The code above uses the TableCell.CellStyle property to set the cell's style.
    4. Create a blue-violet background color for row 0, column 1. Enter the following code:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      table.Cells(0, 1).CellStyle.BackColor = Color.BlueViolet
      

      To write code in C#

      C#
      Copy Code
      table.Cells[0, 1].CellStyle.BackColor = Color.BlueViolet;
      

    Run the program and observe the following:

    Your table should appear similar to the table below: