The main functionality of this helper class is to print the contents of a DataTable
to the Console
. This extension class can also
- Print
DataTable
,DataView
,DataSet
,DataRow[]
(=DataTable.Select()
) - Print Columns
- Print in tabular format and list format
- Print to
Console
,StringBuilder
orStream
All the examples are performed on a products DataTable
. All the DataTable
extension methods in these examples have equivalent extension methods for DataView
, DataSet
and DataRow[]
.
DataTable products = new DataTable("Products");
products.Columns.Add("Id", typeof(int)).AllowDBNull = false;
products.Columns.Add("ProductName", typeof(string));
products.Columns.Add("InStock", typeof(bool));
products.Columns.Add("DateAdded", typeof(DateTime));
Original article of Print DataTable To Console on CodeProject.
Add the helper file to your project. That's it. The namespace, for this extension class, is System.Data
, same as DataTable
and DataView
, so once you start adding these data objects to your project, the helper class works right out of the box.
The Print
extension method prints the DataTable
in a tabular format. The optional parameters are:
rowOrdinals
- Whether to print the ordinal position of the currentDataRow
in theDataTable
.top
- Print only the first top rows. Iftop
is less than or equal to 0 than print all rows.toString
- A delegate that can be used to override the defaultToString()
for certain object types or columns.columnNames
- List of columns to print in a given order. If this parameter is empty than print all the columns. The parameter can accept the same column name several times. For example, printing theId
column twice, as the first column and as the last column.
Print(
this DataTable dataTable,
bool rowOrdinals = false,
int top = 0,
ValueToStringEventHandler toString = null,
params string[] columnNames
);
delegate string ValueToStringEventHandler(object obj, DataRow row, DataColumn column);
This is the simplest print. Prints all products to the console.
products.Print();
This will print the top 5 products with row ordinals, and it will print the Id
column twice on both sides of the table.
products.Print(true, 5, "Id", "DateAdded", "InStock", "Id");
This will print the DateAdded
column in a yyyy-MM-dd
format. Any other column will be printed based on its default ToString()
data type.
products.Print((obj, row, column) =>
{
if (obj == DBNull.Value)
return null;
if (column.ColumnName == "DateAdded")
return ((DateTime)obj).ToString("yyyy-MM-dd");
return obj.ToString();
});
The PrintList
extension method prints the DataTable
in a list format. The default is a 2-columns vertical layout. PrintList
layout the results the same way DataList control display its layout.
The parameters rowOrdinals
, top
, toString
and columnNames
are the same as the Print
parameters.
repeatColumns
- Number of layout columns.repeatDirection
- Vertical or horizontal layout.delimiter
- The delimiter between the column name and the row value.
PrintList(
this DataTable dataTable,
bool rowOrdinals = false,
int top = 0,
ValueToStringEventHandler toString = null,
int repeatColumns = 2,
RepeatDirection repeatDirection = RepeatDirection.Vertical,
string delimiter = ": ",
params string[] columnNames
);
Prints all products with 2-columns vertical layout.
products.PrintList();
Prints Id
and ProductName
columns with 3-columns horizontal layout.
products.PrintList(3, PrintDataExtensions.RepeatDirection.Horizontal, "Id", "ProductName");
The PrintColumns
extension method prints information about the columns of the DataTable
. The output columns are Column Ordinal, Column Name, Data Type, Nullable and Data Member. Data Member is a data member declaration which may come in handy.
PrintColumns(
this DataTable dataTable,
params string[] columnNames
);
Prints the columns of the products DataTable
.
products.PrintColumns();
The default output is to the Console
. However, you can redirect to output to a StringBuilder
or to a Stream
(MemoryStream
, FileStream
). The default encoding for Stream
is Encoding.UTF8
.
You have to redirect the output before printing. The redirection methods are straightforward:
PrintDataExtensions.SetOutputConsole();
PrintDataExtensions.SetOutputStringBuilder(StringBuilder builder);
PrintDataExtensions.SetOutputStream(Stream stream);
PrintDataExtensions.SetOutputStream(Stream stream, Encoding encoding);
Prints to StringBuilder
.
StringBuilder builder = new StringBuilder();
PrintDataExtensions.SetOutputStringBuilder(builder);
products.Print();
Prints to MemoryStream
.
byte[] buffer = null;
using (MemoryStream stream = new MemoryStream())
{
PrintDataExtensions.SetOutputStream(stream);
products.Print();
buffer = stream.ToArray();
}
string output = Encoding.UTF8.GetString(buffer);
Prints to FileStream
.
string path = @"C:\Path\To\output.txt";
using (FileStream stream = File.OpenWrite(path))
{
PrintDataExtensions.SetOutputStream(stream);
products.Print();
}
The characters that make up the border are part of an Extended ASCII characters set. Extended ASCII codes start from code 128. These characters may not necessarily be mapped to box characters, it depends on your computer localization. If the border comes out garbled, you can change the way the border is printed to regular ASCII characters. There is also an option to remove the border completely.
PrintDataExtensions.ExtendedASCIIBorder();
PrintDataExtensions.ASCIIBorder();
PrintDataExtensions.ClearBorder();
You have to set the border before printing.
PrintDataExtensions.ASCIIBorder();
products.Print();