Is there a predefined or ‘easy’ method of writing a datatable to a text file or TextBox Control (With monospace font) such as DataTable.Print():
Column1| Column2| --------|--------| v1| v2| v3| v4| v5| v6|
Edit
Here’s an initial version (vb.net) – in case anyone is interested or wants to build their own:
Public Function BuildTable(ByVal dt As DataTable) As String Dim result As New StringBuilder Dim widths As New List(Of Integer) Const ColumnSeparator As Char = '|'c Const HeadingUnderline As Char = '-'c ' determine width of each column based on widest of either column heading or values in that column For Each col As DataColumn In dt.Columns Dim colWidth As Integer = Integer.MinValue For Each row As DataRow In dt.Rows Dim len As Integer = row(col.ColumnName).ToString.Length If len > colWidth Then colWidth = len End If Next widths.Add(CInt(IIf(colWidth < col.ColumnName.Length, col.ColumnName.Length + 1, colWidth + 1))) Next ' write column headers For Each col As DataColumn In dt.Columns result.Append(col.ColumnName.PadLeft(widths(col.Ordinal))) result.Append(ColumnSeparator) Next result.AppendLine() ' write heading underline For Each col As DataColumn In dt.Columns Dim horizontal As String = New String(HeadingUnderline, widths(col.Ordinal)) result.Append(horizontal.PadLeft(widths(col.Ordinal))) result.Append(ColumnSeparator) Next result.AppendLine() ' write each row For Each row As DataRow In dt.Rows For Each col As DataColumn In dt.Columns result.Append(row(col.ColumnName).ToString.PadLeft(widths(col.Ordinal))) result.Append(ColumnSeparator) Next result.AppendLine() Next Return result.ToString() End Function
No, there isn’t. You will have to do the formatting yourself, or find a third-party library that will do this yourself.