27

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?

HatSoft
  • 11,077
  • 3
  • 28
  • 43
SpaceApple
  • 1,309
  • 1
  • 24
  • 46
  • 1
    Can you please elaborate on how you "noticed this would not work"? What is wrong with it? – Rob H Aug 30 '12 at 13:57

4 Answers4

62
foreach (DataColumn col in rightsTable.Columns)
{
     foreach (DataRow row in rightsTable.Rows)
     {
          Console.WriteLine(row[col.ColumnName].ToString());           
     }
} 
Candide
  • 30,469
  • 8
  • 53
  • 60
  • 3
    DataRow object can receive a DataColumn as a parameter. So instead of row[col.ColumnName], you could simply have row[col]. – jjspierx Aug 09 '17 at 17:38
15
     foreach (DataRow row in dt.Rows) 
     {
        foreach (DataColumn col in dt.Columns)
           Console.WriteLine(row[col]);
     }
oopbase
  • 11,157
  • 12
  • 40
  • 59
  • Problem with that it is going to repeat the column name foreach datarow which I don't want. Basically I want the column to be displayed once and then have all the rows for that column to be presented – SpaceApple Aug 30 '12 at 14:30
9

Please try the following code below:

//Here I am using a reader object to fetch data from database, along with sqlcommand onject (cmd).
//Once the data is loaded to the Datatable object (datatable) you can loop through it using the datatable.rows.count prop.

using (reader = cmd.ExecuteReader())
{
// Load the Data table object
  dataTable.Load(reader);
  if (dataTable.Rows.Count > 0)
  {
    DataColumn col = dataTable.Columns["YourColumnName"];  
    foreach (DataRow row in dataTable.Rows)
    {                                   
       strJsonData = row[col].ToString();
    }
  }
}
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
Chikku Jacob
  • 2,114
  • 1
  • 18
  • 33
4

If you want to change the contents of each and every cell in a datatable then we need to Create another Datatable and bind it as follows using "Import Row". If we don't create another table it will throw an Exception saying "Collection was Modified".

Consider the following code.

//New Datatable created which will have updated cells
DataTable dtUpdated = new DataTable();

//This gives similar schema to the new datatable
dtUpdated = dtReports.Clone();
foreach (DataRow row in dtReports.Rows)
{
    for (int i = 0; i < dtReports.Columns.Count; i++)
    {
        string oldVal = row[i].ToString();
        string newVal = "{"+oldVal;
        row[i] = newVal;
    }
    dtUpdated.ImportRow(row); 
}

This will have all the cells preceding with Paranthesis({)

Guido
  • 46,642
  • 28
  • 120
  • 174
Deepak Kothari
  • 1,601
  • 24
  • 31