Display datarows under each other

How can I loop through the data so that the rows are displayed neatly under each other, in turn.

what I tried is the following, but this is how all the data is displayed in one column.

foreach (DataRow row in myTopTenData.Rows) { foreach (DataColumn col in myTopTenData.Columns) { Console.Write(row[col].ToString() + " "); Console.WriteLine(); } } 
0
source share
3 answers
 foreach (DataRow row in myTopTenData.Rows) { foreach (DataColumn col in myTopTenData.Columns) Console.Write(string.Format("{0, -10}", row[col].ToString())); Console.WriteLine(); } 

string.Format"{0, -10}" helps you align the columns (use negative values ​​to align to the left, positive to align correctly and, of course, 10 is an arbitrary value).

+1
source

You can use this little Linq and String.Join :

 var allFields = myTopTenData.AsEnumerable() .Select(r => string.Join(Environment.NewLine, r.ItemArray)); string allFieldsLines = string.Join(Environment.NewLine, allFields); Console.Write(allFieldsLines); 

Here's a non-Linq version with a loop:

 foreach (DataRow row in myTopTenData.Rows) Console.Write(string.Join(Environment.NewLine, row.ItemArray) + Environment.NewLine); 
+1
source

Why don't you just try:

 foreach (DataRow row in myTopTenData.Rows) { foreach (DataColumn col in myTopTenData.Columns) { Console.Write(row[col].ToString() + " "); } Console.WriteLine(); } 
+1
source

All Articles