Products Technologies Demo Docs Blog Support Company

A Step-by-Step Guide to Formatting Table Cells in TX Text Control Programmatically Using C#

Users can easily insert tables, modify their structure, adjust row and column sizes, and apply formatting to table cells programmatically. This article shows the powerful table features in TX Text Control. It also shows the many ways they can improve your document processing in C#.

A Step-by-Step Guide to Formatting Table Cells in TX Text Control Programmatically Using C#

TX Text Control has strong document processing capabilities beyond simple text editing. It can simplify the creation and manipulation of tables within documents. This makes it very useful to automate documents like banking statements and reports right within your application.

Users can easily insert tables, modify their structure, adjust row and column sizes, and apply formatting to table cells programmatically. This level of control is crucial for managing financial documents. It helps present structured data clearly and professionally.

This article shows the powerful table features in TX Text Control. It also shows the many ways they can improve your document processing in C#.

The following programming categories are discussed in this article:

Tables - TX Text Control .NET for Windows Forms:

Classes
TXTextControl.Table
TXTextControl.TableBaseCollection
TXTextControl.TableCell
TXTextControl.TableCellCollection
TXTextControl.TableCellFormat
TXTextControl.TableCollection

Adding Tables

Start by inserting a simple table through the provided code snippet using the Table class.

// Set table identifier. 
int tableID = 10;  

// Add a new table with number of rows and columns. 
textControl1.Tables.Add(2, 5, tableID); 

// Retrieve the newly added table using the tableID. 
TXTextControl.Table table = textControl1.Tables.GetItem(tableID);

Result:

Adding Tables

Formatting TableCells

In any banking statement, it's essential to add header text to tables for displaying transaction details. Handle and format these headers using TableCell and TableCellFormat classes in TX Text Control.

// Initialize table headers. 
string[] headers = { "Date", "Description", "Credit", "Debit", "Balance" }; 

for (int i = 1; i <= headers.Length; i++) 
{ 
  // Assign header text to each cell in the first row. 
  table.Cells.GetItem(1, i).Text = headers[i - 1]; 

  // Retrieve and select the cell for formatting. 
  TableCell cell = table.Cells.GetItem(1, i); 

  cell.Select(); 

  // Center align the text and set it to bold. 
  textControl1.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center; 
  textControl1.Selection.Bold = true; 

  // Change the text color. 
  textControl1.Selection.ForeColor = Color.Navy; 
}

Result:

Formatting TableCells

Now that the table headers have content, use the TableCellFormat class to improve their visual appeal by formatting the background color and border width.

// Create a new TableCellFormat object to apply formatting. 
TXTextControl.TableCellFormat myCellFormat = new TXTextControl.TableCellFormat(); 

// Set the background color. 
myCellFormat.BackColor = Color.LightGray; 

// Specify a bottom border width. 
myCellFormat.BottomBorderWidth = 50; 

// Apply the formatting. 
for (int i = 1; i <= 5; i++) 
{ 
  table.Cells.GetItem(1, i).CellFormat = myCellFormat; 
}

Result:

Formatting TableCells

Conclusion

Above are a few properties of the TableCellFormat class used for formatting table cells. However, many more are available to customize and automate documents based on your needs. Refer to the image below for further possibilities:

Properties
BackColor
BottomBorderColor
BottomBorderWidth
BottomTextDistance
LeftBorderColor
LeftBorderWidth
LeftTextDistance
NumberFormat
RightBorderColor
RightBorderWidth
RightTextDistance
TextType
TopBorderColor
TopBorderWidth
TopTextDistance
VerticalAlignment

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.Table Class
  • TXTextControl.TableCell Class
  • TXTextControl.TableCellFormat Class
  • TXTextControl.Selection Class

Windows Forms

Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.

See Windows Forms products

Related Posts

ASP.NETWindows FormsASP.NET Core

Splitting Tables at Bookmark Positions and Cloning Table Headers

This article shows how to split tables at bookmark positions and how to clone table headers in TX Text Control .NET for Windows Forms and TX Text Control .NET Server.


ASP.NETWindows FormsExcel

Loading and Processing Excel XLSX Spreadsheet Tables into TX Text Control…

TX Text Control provides a powerful API to load and process Excel spreadsheet tables in .NET applications. This article shows how to load an Excel file and process the tables using TX Text Control…


ASP.NETWindows FormsTab Stops

Text to Table and Table to Text in TX Text Control and C#

TX Text Control provides powerful table features and also full access to text formatting which can be used to create tables from text and vice versa. This article shows how to convert text to…


ASP.NETWindows FormsWPF

Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#

This article shows how to insert MergeBlocks with the DataSourceManager and how to apply table styles to those tables. The article uses the DocumentServer class to insert MergeBlocks with the…


ASP.NETWindows FormsWPF

Useful Tricks for Working with Tables

When you are working with complex and long tables, it can be useful to know if a table is broken up across multiple pages. These code snippets will help you with various table related tasks.