jak wypisać kolumny z bazy za pomocą polecenia Console.WriteLine, żeby były one ładnie ułożone w kolumny. za pomocą \t sa one oddzielone ale całość jest "rozjechana" na ekranie
0
0
To mój stary kod z biblioteki, której już dziś nie używam, więc metoda może być dalece niedoskonałą.
public static void DebugPrintTable(DataTable table)
{
StringBuilder sb = new StringBuilder();
int colNr = 0;
int colCount = table.Columns.Count;
int nLine = 0;
int n = 10;
foreach (DataColumn col in table.Columns)
{
colNr++;
sb.AppendFormat("{0,-" + n + "}", col);
if (colNr < colCount)
{
sb.Append("|");
}
nLine += n;
}
Console.WriteLine(sb.ToString());
sb.Remove(0, sb.Length);
Console.WriteLine(sb.ToString().PadLeft(nLine, '-'));
sb.Remove(0, sb.Length);
int rowNr = 0;
foreach (DataRow row in table.Rows)
{
rowNr++;
int itemNr = 0;
foreach (object obj in row.ItemArray)
{
itemNr++;
sb.AppendFormat("{0,-" + n + "}", obj);
if (itemNr < colCount)
{
sb.Append("|");
}
}
System.Console.WriteLine(sb.ToString());
sb.Remove(0, sb.Length);
}
Console.WriteLine(sb.ToString().PadLeft(nLine, '-'));
sb.Remove(0, sb.Length);
}
--
CORESOFT LAB
Marcin Zamorski