ComponentOne Preview Classic for WinForms: Preview Classic for WinForms Quick Start > Creating Tables in C1PrintDocument > Creating a Background Color for Specific Cells in the Table

Creating a Background Color for Specific Cells in the Table

Creating a background color for specific cells in the table demonstrates how to create background colors for specific cells in the table. It also demonstrates how to use the StyleTableCell property to set the styles used in the table that will be rendered. This topic assumes you have a table with three columns and three rows.

1.   The following code below 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.   Add the following code below the AddSome method listed above. This code will create a crimson background color for row 1, column 2.

      Visual Basic

table.Body.Cell(1, 2).StyleTableCell.BackColor = Color.Crimson

      C#

table.Body.Cell(1, 2).StyleTableCell.BackColor = Color.Crimson;

Note: The rows and columns begin with 0. The code above uses the StyleTableCell property to set the cell's style.

3.   Create a blue-violet background color for row 0, column 1. Enter the following code:

      Visual Basic

table.Body.Cell(0, 1).StyleTableCell.BackColor = Color.BlueViolet

      C#

table.Body.Cell(0, 1).StyleTableCell.BackColor = Color.BlueViolet;

Run the program and observe the following:

Your C1PrintDocument form will appear with one crimson and one blue-violet cell, like this:

 


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