Display data table data

I want to display values ​​or write them to a text file that it writes as such from a dataset

ColumnID columnName ColumnFamilyName ValueOne ValueTwo ValueThree ValueThree ValueFour ValueFive ValueSix ValueSeven ValueEight 

I did it that doesn't do the trick

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

How can i do this?

+6
source share
3 answers

I still can't post a comment, but here is a quick answer:

  foreach(DataRow row in myTopTenData.Rows) { string ID = row["ColumnID"].ToString(); string Name= row["columnName"].ToString(); string FamilyName= row["ColumnFamilyName"].ToString(); } 

Be sure to check for null values ​​when retrieving data

+11
source

Assuming myTopTenData is a DataTable, then you loop in datatable strings this way

 foreach (DataRow row in myTopTenData.Rows) { Console.WriteLine(); for(int x = 0; x < myTopTenData.Columns.Count; x++) { Console.Write(row[x].ToString() + " "); } } 

Of course, this fragment should be considered only as a trivial example.
You need to consider zero values ​​and reliable error checking.

+9
source

You can use Datagrid to display your DataTable as follows:

in wpf:

 datagrid.SetBinding(ItemsControl.ItemsSourceProperty, new System.Windows.Data.Binding { Source = dt}); 

==================================================== ========================

in winform:

 datagrid.DataSource(datatable); 
+1
source

All Articles