| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

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

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.
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 Name | Type |
| company | string |
| recipient | string |
| street | string |
| city | string |
| country | string |
| salutation | string |
The sample class that encapsulates the table is called DataGridTable and must be initialized with 3 parameters in it's constructor:
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 | |
|---|---|
| Property | Type |
| Bold | bool |
| Italic | bool |
| FontName | string |
| FontSize | int |
| Height | int |
| FontColor | System.Drawing.Color |
| Alignment | TXTextControl.HorizontalAlignment |
| UnderlineStyle | TXTextControl.FontUnderlineStyle |
| Inheritet Members | |
| Property | Type |
| BackColor | System.Drawing.Color |
| BottomBorderWidth | int |
| BottomTextDistance | int |
| LeftBorderWidth | int |
| LeftTextDistance | int |
| RightBorderWidth | int |
| RightTextDistance | int |
| TopBorderWidth | int |
| TopTextDistance | int |
| VerticalAlignment | TXTextControl.VerticalAlignment |
First, a new instance of the DataGridTable must be created:
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.
TableRowFormat myRowFormat = new TableRowFormat(); myRowFormat.BackColor = System.Drawing.Color.LightGray; myRowFormat.Alignment = TXTextControl.HorizontalAlignment.Center; myRowFormat.VerticalAlignment = TXTextControl.VerticalAlignment.Center; myTable.RowFormat = myRowFormat; TableRowFormat myAlternateRowFormat = new TableRowFormat(); myAlternateRowFormat.BackColor = Color.BlanchedAlmond; myAlternateRowFormat.FontColor = Color.Brown; myAlternateRowFormat.VerticalAlignment = TXTextControl.VerticalAlignment.Center; myAlternateRowFormat.Alignment = TXTextControl.HorizontalAlignment.Center; myTable.AlternateRowFormat = myAlternateRowFormat; TableRowFormat myHeaderFormat = new TableRowFormat(); myHeaderFormat.Height = 600; myHeaderFormat.Alignment = TXTextControl.HorizontalAlignment.Center; myHeaderFormat.VerticalAlignment = TXTextControl.VerticalAlignment.Center; myHeaderFormat.BottomBorderWidth = 1; myTable.HeaderRowFormat = myHeaderFormat;
Finally, the table can be added, using the Add method of the DataGridTable class.
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.