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 / Adding Text to Table Cells
In This Topic
    Adding Text to Table Cells
    In This Topic

    This topic shows how to use the RenderText class to add text into specific cells of the table.

    1. The following code to set up a table with dark gray gridlines 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)
      
          ' 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);
          
          // Generate the document.   
          this.c1PrintDocument1.Body.Children.Add(table);    
          this.c1PrintDocument1.Generate();
          
      }
      
    2. Showing any kind of content in a table cell is done by assigning the render object representing that content to the cell's RenderObject property. But, because showing text in table cells is such a common task, cells have an additional specialized property RenderText that we will use. In order to set the texts of all cells in the table, you need to loop over the table's rows, and inside that loop do another loop over the row's columns. In the body of the nested loop set the Text property to the desired text as follows (for the sake of this sample, we leave cells (1,1) and (1,2) empty):

      To write code in Visual Basic

      Visual Basic
      Copy Code
      ' Add 3 rows.    
      Dim r As Integer = 3
      
      ' Add 3 columns.    
      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    
              If (Not (row = 1 And col = 1)) And (Not (row = 1 And col = 2)) Then    
                 Dim celltext As C1.C1Preview.RenderText = New C1.C1Preview.RenderText(Me.C1PrintDocument1)    
                 celltext.Text = String.Format("Cell ({0},{1})", row, col)
      
                 ' Add cells with text.    
                 table.Cells(row, col).RenderObject = celltext    
             End If    
          Next    
      Next
      

      To write code in C#

      C#
      Copy Code
      // Add 3 rows.    
      const int r = 3;
      
      // Add 3 columns.    
      const int c = 3;
      
      for (int row = 0; row < r; ++row)    
      {    
          for (int col = 0; col < c; ++col)   
          {    
              if (!(row == 1 && col == 1) && !(row == 1 && col == 2))    
              {    
                  C1.C1Preview.RenderText celltext = new C1.C1Preview.RenderText(this.c1PrintDocument1);    
                  celltext.Text = string.Format("Cell ({0}, {1})", row, col);
      
                  // Add cells with text.    
                  table.Cells[row, col].RenderObject = celltext;    
              }    
          }    
      }
      

    Run the program and observe the following:

    Your table should look similar to the table below: