Bassem Mohsen's Programming Blog

"High thoughts must have high language." —Aristophanes

  • The Author

    Bassem Mohsen - Freelance Windows and
    Web application developer. Technical writer. 24 yo. Based in Cairo, Egypt.

  • MCTS certification logo

Archive for August, 2011

DataTable Formatter

Posted by Bassem Mohsen on August 6, 2011

Download the DataTableFormatterProj project containing the DataTableFormatter class and test code that shows how to use it.

Introduction

It happened three times that I needed to display my DatatTable objects in an unusual way (the usual way is to display the DataTable object using WPF, Windows Forms or ASP.NET controls by binding the DataTable to the control that should display it). One time I needed to display DataTables in a console application. This means that DataTables will have to be displayed to the text-based console screen using characters only. Another time I needed my program to generate HTML pages containing DataTables formatted using HTML tables. A third time, I needed to print the DataTables (I think this is a common scenario). So I decided to make my program generate flow documents (flow documents are easily printable) containing flow document Tables to display my DataTables.

I recently got the idea of collecting the three DataTable formatter methods I wrote previously in a single, reusable class. I called the class DataTableFormatter.

DataTableFormatter Members

DataTableFormatter is a static class containing three methods.

  Name Description
public-method static GetFlowDocumentTable Gets a flow document Table object representing the DataTable object and its contents.
public-method static GetHtmlTable Gets an HTML ‘table’ element representing the DataTable object and its contents.
public-method static GetStringRepresentation Gets a string representation of the DataTable.

GetStringRepresentation(DataTable) is useful when you want to save your DataTable to a text file, print the DataTable to the console screen, or display it in a text-based control like a TextBox.

The following image shows the output string of GetStringRepresentation printed to the console screen.

datatable-formatter-string-representation

I should admit that this table style is cheated from the MySQL command line client.

GetHtmlTable(DataTable) is useful when you want to display your DataTable in an HTML page. I used it in a program that generated HTML pages as output. But I think that, with small modifications, it can also be useful for ASP.NET MVC applications.

The following image shows the output HTML of GetHtmlTable displayed in a WPF WebBrowser control.

datatable-formatter-html-table

Finally, GetFlowDocumentTable(DataTable) creates a flow document table based on your DataTable. You will need to insert the created Table in a flow document which is then displayed in your application or, more commonly, printed.

The following image shows a flow document containing a Table created by GetFlowDocumentTable. The flow document is displayed by a FlowDocumentScrollViewer

datatable-formatter-flow-document-table

Download the DataTableFormatterProj project containing the DataTableFormatter class and test code that shows how to use it.

Posted in Small Software Projects | Tagged: , , | Leave a Comment »