Creating a Table with Three Columns and Rows in C1PrintDocument
Creating a table with three columns and rows in illustrates the basics of setting up a table using the RenderTable class.
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.
2. From the Toolbox, select and drop a C1PrintPreview control and a C1PrintDocument component onto the form.
3. Set the C1PrintPreview's Dock property to Fill (the preview will be the only control on our form). Make the C1PrintPreview show the C1PrintDocument by setting the preview's Document property to the C1PrintDocument.
4. Double-click the empty form. This will automatically create a new handler for the Form_Load event in the source code. The handler will look like the following:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
• C#
private void Form1_Load(object sender, System.EventArgs e)
{
}
5. Second, create a new C1.C1PrintDocument.RenderTable object and assign it to a variable by adding the following code to the Form_Load event:
Dim table As C1.C1PrintDocument.RenderTable = New C1.C1PrintDocument.RenderTable(Me.C1PrintDocument1)
• C#
C1.C1PrintDocument.RenderTable table = new C1.C1PrintDocument.RenderTable(this.c1PrintDocument1);
Note: The code above will need to be inserted within the Form1_Load event.
6. Now, add three columns to the table and three rows to the table's body using the AddSome method. The code is as follows:
' Add three columns to the table.
table.Columns.AddSome(3)
' Add three rows to the table.
table.Body.Rows.AddSome(3)
• C#
// Add three columns to the table.
table.Columns.AddSome(3);
// Add three rows to the table.
table.Body.Rows.AddSome(3);
Note: The code above should follow the code entered in step 5.
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.
7. Make the columns' width and rows' height six centimeters long:
table.Body.Rows(0).HeightStr = "6cm"
table.Columns(0).WidthStr = "6cm"
table.Columns(1).WidthStr = "6cm"
table.Body.Rows(1).HeightStr = "6cm"
table.Columns(2).WidthStr = "6cm"
table.Body.Rows(2).HeightStr = "6cm"
• C#
table.Body.Rows[0].HeightStr = "6cm";
table.Columns[0].WidthStr = "6cm";
table.Columns[1].WidthStr = "6cm";
table.Body.Rows[1].HeightStr = "6cm";
table.Columns[2].WidthStr = "6cm";
table.Body.Rows[2].HeightStr = "6cm";
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 StartDoc method on the document, then add content to the document by calling any of the Render methods (there are a number of methods for rendering different things; in our example we will use the general RenderBlock method which allows to add ("render") any RenderObject), and finally to call the EndDoc method to end the document generation. Here is the code:
Me.C1PrintDocument1.StartDoc()
Me.C1PrintDocument1.RenderBlock(table)
Me.C1PrintDocument1.EndDoc()
• C#
this.c1PrintDocument1.StartDoc();
this.c1PrintDocument1.RenderBlock(table);
this.c1PrintDocument1.EndDoc();
Run the program and observe the following:
Your application will appear similar to the image below at run time:
|