ComponentOne Reports for WinForms Designer Edition: ComponentOne Reports for WinForms > Getting Started with Printing and Previewing > C1PrintDocument Quick Start > Creating Tables > Creating a Table with Three Columns and Rows

Creating a Table with Three Columns and Rows

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 C1PrintPreviewControls Dock property to Fill (the preview will be the only control on our form). Set the C1PrintPreviewControls Document property to the C1PrintDocument 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:

      Visual Basic

Dim table As C1.C1Preview.RenderTable = New C1.C1Preview.RenderTable(Me.C1PrintDocument1)

      C#

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:

      Visual Basic

' 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

      C#

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

      Visual Basic

table.Height = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm)

table.Width = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm)

      C#

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:

      Visual Basic

table.Style.GridLines.All = New C1.C1Preview.LineDef(Color.DarkGray)

      C#

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:

      Visual Basic

Me.C1PrintDocument1.Body.Children.Add(table)

Me.C1PrintDocument1.Generate()

      C#

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:

 


Adding Text to Table Cells

Adding Two Images to Specific Cells of the Table

Creating Borders Around Rows and Columns in Your Table

Creating a Background Color for Specific Cells in the Table


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.