Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through a DataTable

Well. I have a DataTable with multiple columns and multiple rows.

I want to loop through the DataTable dynamically basically the output should look as follows excluding the braces :

Name (DataColumn)
Tom  (DataRow)
Peter (DataRow)

Surname (DataColumn)
Smith (DataRow)
Brown (DataRow)

foreach (DataColumn col in rightsTable.Columns)
{
     foreach (DataRow row in rightsTable.Rows)
     {
          //output              
     }
} 

I typed that out and noticed this would not work. Can someone please advice on a better way of doing this?

like image 563
SpaceApple Avatar asked Aug 30 '12 13:08

SpaceApple


People also ask

How do I find DataTable data?

You can use rows(). data() to get the data for the selected rows.


2 Answers

foreach (DataColumn col in rightsTable.Columns)
{
     foreach (DataRow row in rightsTable.Rows)
     {
          Console.WriteLine(row[col.ColumnName].ToString());           
     }
} 
like image 98
Candide Avatar answered Oct 19 '22 12:10

Candide


     foreach (DataRow row in dt.Rows) 
     {
        foreach (DataColumn col in dt.Columns)
           Console.WriteLine(row[col]);
     }
like image 14
oopbase Avatar answered Oct 19 '22 12:10

oopbase