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
In This Topic
    Creating a Table with Three Columns and Rows
    In This Topic

    This topic illustrates the basics of setting up a table with three rows and three columns. Complete the following steps:

    1. First, set up the basic framework for the sample that will allow generating and previewing the document. Create a new .NET Windows Application project. Add a C1PrintPreviewControl and a C1PrintDocument component on the form.
    2. Set the C1PrintPreviewControl Dock property to Fill (the preview will be the only control on our form). Set the C1PrintPreviewControl Document property to the C1PrintDocument1 as shown in the following picture:
         
      This will make the C1PrintPreviewControl show the C1PrintDocument.
    3. Double-click the form's title bar to switch to Code view and create a new handler for the Form_Load event in the source code.
    4. Second, create a new C1.C1PrintDocument.RenderTable object and assign it to a variable by adding the following code to the Form1_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim table As C1.C1Preview.RenderTable = New C1.C1Preview.RenderTable(Me.C1PrintDocument1)

      To write code in C#

      C#
      Copy Code
      C1.C1Preview.RenderTable table = new C1.C1Preview.RenderTable(this.c1PrintDocument1);
    5. Now, add 3 columns to the table and 3 rows to the table's body by adding the following code after the code added in the previous step:

      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    
              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
      

      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)   
          {   
              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;    
          }    
      }
      

      Note also that while we added columns "directly" to the table, rows were added to the table's body. That is because a RenderTable always consists of 3 "bands": Header, Body and Footer. Any of the three bands may be empty in the table. If you just want a simple table you can add rows to the body as we do in this example.

    6. Make the table's width and height fifteen centimeters long, by adding the following code:

      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);
      
    7. By default, tables have no borders. Add a dark gray gridlines to the table:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      table.Style.GridLines.All = New C1.C1Preview.LineDef(Color.DarkGray)
      

      To write code in C#

      C#
      Copy Code
      table.Style.GridLines.All = new C1.C1Preview.LineDef(Color.DarkGray);
      
    8. When you have created the render object(s), you need to add them to your document. The way to do it is to first call the Add method on the document to add the table to the body of the document, and then use the Generate method to create the document. Here is the code:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1PrintDocument1.Body.Children.Add(table)    
      Me.C1PrintDocument1.Generate()
      

      To write code in C#

      C#
      Copy Code
      this.c1PrintDocument1.Body.Children.Add(table);    
      this.c1PrintDocument1.Generate();
      

    Run the program and observe the following:

    Your application will appear similar to the image below at run time:

    See Also