How to 'foreach' a column in a DataTable using C#?

C#Foreach

C# Problem Overview


How do I loop through each column in a datarow using foreach?

DataTable dtTable = new DataTable();
MySQLProcessor.DTTable(mysqlCommand, out dtTable);

foreach (DataRow dtRow in dtTable.Rows)
{
    //foreach(DataColumn dc in dtRow)
}

C# Solutions


Solution 1 - C#

This should work:

DataTable dtTable;

MySQLProcessor.DTTable(mysqlCommand, out dtTable);

// On all tables' rows
foreach (DataRow dtRow in dtTable.Rows)
{
	// On all tables' columns
	foreach(DataColumn dc in dtTable.Columns)
	{
	  var field1 = dtRow[dc].ToString();
	}
}

Solution 2 - C#

I believe this is what you want:

DataTable dtTable = new DataTable();
foreach (DataRow dtRow in dtTable.Rows)
{
    foreach (DataColumn dc in dtRow.ItemArray)
    {

    }
}

Solution 3 - C#

You can do it like this:

DataTable dt = new DataTable("MyTable");

foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn column in dt.Columns)
    {
        if (row[column] != null) // This will check the null values also (if you want to check).
        {
               // Do whatever you want.
        }
     }
}

Solution 4 - C#

You can check this out. Use foreach loop over a DataColumn provided with your DataTable.

 foreach(DataColumn column in dtTable.Columns)
 {
     // do here whatever you want to...
 }

Solution 5 - C#

Something like this:

 DataTable dt = new DataTable();

 // For each row, print the values of each column.
    foreach(DataRow row in dt .Rows)
    {
        foreach(DataColumn column in dt .Columns)
        {
            Console.WriteLine(row[column]);
        }
    }

http://msdn.microsoft.com/en-us/library/system.data.datatable.rows.aspx

Solution 6 - C#

In LINQ you could do something like:

foreach (var data in from DataRow row in dataTable.Rows
                     from DataColumn col in dataTable.Columns
                          where
                              row[col] != null
                          select row[col])
{
    // do something with data
}

Solution 7 - C#

int countRow = dt.Rows.Count;
int countCol = dt.Columns.Count;

for (int iCol = 0; iCol < countCol; iCol++)
{
     DataColumn col = dt.Columns[iCol];

     for (int iRow = 0; iRow < countRow; iRow++)
     {
         object cell = dt.Rows[iRow].ItemArray[iCol];

     }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser222427View Question on Stackoverflow
Solution 1 - C#Davide PirasView Answer on Stackoverflow
Solution 2 - C#glosrobView Answer on Stackoverflow
Solution 3 - C#Glory RajView Answer on Stackoverflow
Solution 4 - C#jleView Answer on Stackoverflow
Solution 5 - C#Kamil LachView Answer on Stackoverflow
Solution 6 - C#MarthijnView Answer on Stackoverflow
Solution 7 - C#Luca ZieglerView Answer on Stackoverflow