ComponentOne Preview Classic for WinForms: Preview Classic for WinForms Quick Start > Creating Tables in C1PrintDocument > Adding Text to Table Cells

Adding Text to Table Cells

Adding text to table cells shows how to use the RenderText class to add text into specific cells of the table. This topic assumes you already have created a table with three columns and three rows.

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

      Visual Basic

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

   

    ' Make a table with 3 columns and 3 rows.

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

    table.Columns.AddSome(3)

    table.Body.Rows.AddSome(3)

 

    ' Generate the document.

    Me.C1PrintDocument1.StartDoc()

    Me.C1PrintDocument1.RenderBlock(table)

    Me.C1PrintDocument1.EndDoc()

End Sub

      C#

private void Form1_Load(object sender, System.EventArgs e)

{

    // Make a table with 3 columns and 3 rows.

    C1.C1PrintDocument.RenderTable table = new C1.C1PrintDocument.RenderTable(this.c1PrintDocument1);

    table.Columns.AddSome(3);

    table.Body.Rows.AddSome(3);

 

    // Generate the document.

    this.c1PrintDocument1.StartDoc();

    this.c1PrintDocument1.RenderBlock(table);

    this.c1PrintDocument1.EndDoc();

}

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 RenderText.Text property to the desired text as follows (for the sake of this sample, we leave cells (1,1) and (1,2) empty). Insert the following code between the AddSome and StartDoc methods:

      Visual Basic

Dim i As Integer = 0

While i < table.Body.Rows.Count

    Dim j As Integer = 0

    While j < table.Columns.Count

        If (Not (i = 1 And j = 1)) And (Not (i = 1 And j = 2)) Then

            table.Body.Cell(i, j).RenderText.Text = String.Format("Cell {0},{1}", i, j)

        End If

        j = j + 1

    End While

i = i + 1

End While

      C#

for (int i = 0; i < table.Body.Rows.Count; ++i)

    for (int j = 0; j < table.Columns.Count; ++j)

        if (!(i == 1 && j == 1) && !(i == 1 && j == 2))

            table.Body.Cell(i, j).RenderText.Text = string.Format("Cell {0},{1}", i, j);

Run the program and observe the following:

Your table should look similar to the table below:

 


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