How can I loop through a datatable so that the rows display neatly underneath one another,row by row.
what I have tried is the following but like this all the data displays in one column.
foreach (DataRow row in myTopTenData.Rows)
{
foreach (DataColumn col in myTopTenData.Columns)
{
Console.Write(row[col].ToString() + " ");
Console.WriteLine();
}
}
You can use this little Linq query and
String.Join
:Here's the non-Linq version with a loop:
string.Format"{0, -10}"
will help you to align your columns (use negative values for a left alignment, positive for a right alignement, and of course 10 is an arbitrary value).Why don't you just try with this: