Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Bound tables: A class to fill and format tables easily

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:C#
Version:1.0
Released:January 10, 2007
Last modified:January 11, 2008
Requirements:TX Text Control .NET with C#
Download code:tx_sample_data_table.zip Download [12.99 KB, ZIP]
Click to enlarge

TX Text Control offers a very powerful set of features to insert and manipulate tables. Every table cell can be formatted using the TableCell class.

The formatting of text can be specified, just as any other text element in the document, using the Selection class.

A self-populating bound table class

Consider a table that can be populated with information from a database automatically. This sample introduces a class that can be created with a DataTable in it's constructor. The table adapts the structure of the table automatically and populates it's rows with the content from a database.

In this sample, the following database table structure is loaded into a DataSet:

address_record
Column NameType
companystring
recipientstring
streetstring
citystring
countrystring
salutationstring

The sample class that encapsulates the table is called DataGridTable and must be initialized with 3 parameters in it's constructor:

  1. public DataGridTable(TXTextControl.TextControl tx, DataTable ds, int ID)

The first parameter specifies the TextControl object into which the table will be inserted. The table is inserted at the current input position. The second parameter accepts a DataTable of a specific DatSet which will be used to create the structure of the table and to populate the data. The third parameter is a unique identifier for the new table.

The class provides three public properties to specify the format of a table row, an alternate style for the rows and the format of the table header. The structure TableRowFormat is in fact a class to inherit from the TableCellFormat of the TXTextControl namespace. The new class, however, contains many more formatting options and wraps all character-based, paragraph-based and table cell-based formatting options in one easy to use common class.

The following class diagram shows the options to format the table rows:

class TableRowFormat: TableCellFormat
PropertyType
Boldbool
Italicbool
FontNamestring
FontSizeint
Heightint
FontColorSystem.Drawing.Color
AlignmentTXTextControl.HorizontalAlignment
UnderlineStyleTXTextControl.FontUnderlineStyle
Inheritet Members
PropertyType
BackColorSystem.Drawing.Color
BottomBorderWidthint
BottomTextDistanceint
LeftBorderWidthint
LeftTextDistanceint
RightBorderWidthint
RightTextDistanceint
TopBorderWidthint
TopTextDistanceint
VerticalAlignmentTXTextControl.VerticalAlignment

Create a new instance of DataGridTable

First, a new instance of the DataGridTable must be created:

  1. DataGridTable myTable = new DataGridTable(textControl1, ds.Tables[0], 13);

Additionally, different styles can be created for the rows, an alternate row style and a table header. The table header is only inserted, if a style has been specified. The alternate row style inherits from the normal row style, if no alternate row style is specified.

  1. TableRowFormat myRowFormat = new TableRowFormat();
  2. myRowFormat.BackColor = System.Drawing.Color.LightGray;
  3. myRowFormat.Alignment = TXTextControl.HorizontalAlignment.Center;
  4. myRowFormat.VerticalAlignment = TXTextControl.VerticalAlignment.Center;
  5. myTable.RowFormat = myRowFormat;
  6.  
  7. TableRowFormat myAlternateRowFormat = new TableRowFormat();
  8. myAlternateRowFormat.BackColor = Color.BlanchedAlmond;
  9. myAlternateRowFormat.FontColor = Color.Brown;
  10. myAlternateRowFormat.VerticalAlignment = TXTextControl.VerticalAlignment.Center;
  11. myAlternateRowFormat.Alignment = TXTextControl.HorizontalAlignment.Center;
  12. myTable.AlternateRowFormat = myAlternateRowFormat;
  13.  
  14. TableRowFormat myHeaderFormat = new TableRowFormat();
  15. myHeaderFormat.Height = 600;
  16. myHeaderFormat.Alignment = TXTextControl.HorizontalAlignment.Center;
  17. myHeaderFormat.VerticalAlignment = TXTextControl.VerticalAlignment.Center;
  18. myHeaderFormat.BottomBorderWidth = 1;
  19. myTable.HeaderRowFormat = myHeaderFormat;

Finally, the table can be added, using the Add method of the DataGridTable class.

  1. myTable.AddTable();

The source code for the DataGridTable class is shipped with the sample code. Feel free to extend the functionality of this class.

The minimum requirements for this sample application are TX Text Control .NET trial version and Visual Studio .NET 2005.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place