Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying data of data table

I want to display values or write them to a text file that it is written as such from a data set

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

I have done this which does not 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?

like image 907
Arianule Avatar asked Jan 17 '13 10:01

Arianule


2 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();    
 }

Make sure to check for null values when retrieving the data

like image 177
noobob Avatar answered Sep 17 '22 13:09

noobob


Assuming that myTopTenData is a DataTable then you loop on rows of a datatable in 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 snippet should be taken only as a trivial example.
You need to consider null values and a robust error checking.

like image 29
Steve Avatar answered Sep 20 '22 13:09

Steve