Reports for WinForms | ComponentOne
Getting Started with Reports for WinForms / Getting Started with Printing and Previewing / Creating Tables
In This Topic
    Creating Tables
    In This Topic

    This topic describes how to create a simple table and a table with three columns and rows, add text to cells in the table, add images to specific cells in the table, add text below the table in your document, create borders around rows and columns in the table, and create a background color for specific cells in the table.

    Note: The following topics use the sample application created in the C1PrintDocument Quick Start topic as a base.

    Making a Simple Table:

    Tables provide one of the most useful features in documents. They can be used for both tabular presentation of data, and for layout of other elements of a document. C1PrintDocument provides full-featured tables. In this section you will learn how to start using tables. We will use the "Hello, World!" sample application created in the C1PrintDocument Quick Start topic as our base, and add a table to it.

    Note: The sample code fragments in this topic assume that the "using C1.C1Preview" directive (in C# syntax; or an equivalent for other languages) has been inserted into the file, so that instead of fully qualified type names (such as C1.C1Preview.RenderText) we can use just the class name part (RenderText).
    1. Open the HelloWorld application created in the C1PrintDocument Quick Start topic (alternatively, you can create a new application as described in the previous section).
    2. Switch to code view and in the Form_Load event handler (create it if necessary) add the following code before the call to Generate method:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim rt As New RenderTable()    
      Me.C1PrintDocument1.Body.Children.Add(rt)
      
      Dim row As Integer = 0    
      Do While (row < 10)    
          Dim col As Integer = 0    
          Do While (col < 6)   
              rt.Cells.Item(row, col).Text = String.Format("Cell ({0},{1})", row, col)    
              col += 1   
          Loop    
          row += 1    
      Loop
      

      To write code in C#

      C#
      Copy Code
      RenderTable rt = new RenderTable();    
      this.c1PrintDocument1.Body.Children.Add(rt);
      
      for (int row = 0; row < 10; ++ row)    
      {    
          for (int col = 0; col < 6; ++ col)   
          {   
              rt.Cells[row, col].Text = string.Format("Cell ({0},{1})", row, col);   
          }   
      }
      
    3. Do not forget to call the Generate method on the document.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1PrintDocument1.Generate()
      

      To write code in C#

      C#
      Copy Code
      this.c1PrintDocument1.Generate();
      

    Run the program and observe:

    The preview will show the document similar to the one in the following picture:

    This simple example shows several important aspects of using tables in C1PrintDocument:

    For your reference, here is the complete text of the form load event handler that produced the above document:

    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    
        Me.C1PrintDocument1.Body.Children.Add(New RenderText("Hello, World!"))    
        Dim rt As New RenderTable()    
        Me.C1PrintDocument1.Body.Children.Add(rt)
    
    Dim row As Integer = 0    
    Do While (row < 10)   
        Dim col As Integer = 0   
        Do While (col < 6)   
            rt.Cells.Item(row, col).Text = String.Format("Cell ({0},{1})", row, col)   
            col += 1   
        Loop   
        row += 1   
    Loop   
        rt.Cells(3, 4).Text = "A long line of text showing that table rows " + "stretch to accommodate all content."    
        rt.Cells(10, 7).Text = "text at row 10, column 7"    
        rt.Style.GridLines.All = LineDef.Default   
        Me.C1PrintDocument1.Generate()   
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void Form1_Load(object sender, EventArgs e)    
    {   
        this.c1PrintDocument1.Body.Children.Add(new RenderText("Hello, World!"));    
        RenderTable rt = new RenderTable();    
        this.c1PrintDocument1.Body.Children.Add(rt);   
        for (int row = 0; row < 10; ++row)   
        {    
            for (int col = 0; col < 6; ++col)   
            {   
                rt.Cells[row, col].Text = string.Format("Cell ({0},{1})", row, col);    
            }   
        }   
        rt.Cells[3, 4].Text = "A long line of text showing that table rows " + "stretch to accommodate all content.";    
        rt.Cells[10, 7].Text = "text at row 10, column 7";   
        rt.Style.GridLines.All = LineDef.Default;   
        this.c1PrintDocument1.Generate();    
    }
    

    See Also