Я рекомендую вам ознакомиться с моей статьей DataTable Formatter .
Мой класс DataTableFormatter содержит методы для форматирования DataTables в виде символьных таблиц (ваше требование), таблиц HTML или таблиц потоковых документов.
Вы можете скачать проект, содержащий класс, с моего сайта, но для удобства я выложу код здесь.
/// <summary>
/// Gets a string representation of the <see cref="System.Data.DataTable" />.
/// </summary>
/// <remarks>The string representation should be displayed with a monospaced font.</remarks>
public static string GetStringRepresentation(DataTable dataTable)
{
if (dataTable == null)
throw new ArgumentNullException("'dataTable' cannot be null.");
StringWriter representationWriter = new StringWriter();
// First, set the width of every column to the length of its largest element.
int[] columnWidths = new int[dataTable.Columns.Count];
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
int headerWidth = dataTable.Columns[columnIndex].ColumnName.Length;
int longestElementWidth = dataTable.AsEnumerable()
.Select((row) => row[columnIndex].ToString().Length)
.Max();
columnWidths[columnIndex] = Math.Max(headerWidth, longestElementWidth);
}
// Next, write the table
// Write a horizontal line.
representationWriter.Write("+-");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
for (int i = 0; i < columnWidths[columnIndex]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (columnIndex != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
// Print the headers
representationWriter.Write("| ");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
string header = dataTable.Columns[columnIndex].ColumnName;
representationWriter.Write(header);
for (int blanks = columnWidths[columnIndex] - header.Length; blanks > 0; blanks--)
representationWriter.Write(" ");
representationWriter.Write(" | ");
}
representationWriter.WriteLine();
// Print another horizontal line.
representationWriter.Write("+-");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
for (int i = 0; i < columnWidths[columnIndex]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (columnIndex != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
// Print the contents of the table.
for (int row = 0; row < dataTable.Rows.Count; row++)
{
representationWriter.Write("| ");
for (int column = 0; column < dataTable.Columns.Count; column++)
{
representationWriter.Write(dataTable.Rows[row][column]);
for (int blanks = columnWidths[column] - dataTable.Rows[row][column].ToString().Length;
blanks > 0; blanks--)
representationWriter.Write(" ");
representationWriter.Write(" | ");
}
representationWriter.WriteLine();
}
// Print a final horizontal line.
representationWriter.Write("+-");
for (int column = 0; column < dataTable.Columns.Count; column++)
{
for (int i = 0; i < columnWidths[column]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (column != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
return representationWriter.ToString();
}
Метод GetStringRepresentation (DataTable) дает такие результаты, как этот
+------------------+----------------+-------------------+
| Item | Units in Stock | Unit Price |
+------------------+----------------+-------------------+
| Drilling machine | 1000 | $1,000,000 |
| Backpack | 320 | $24 |
| Chocolate bar | 100000 | $2.00000000000000 |
+------------------+----------------+-------------------+
Я должен признать, что этот стиль таблицы обманут из клиента командной строки MySQL.