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

8

62

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)
}
Ency answered 29/10, 2011 at 13:21 Comment(0)
N
94

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();
    }
}
Notice answered 29/10, 2011 at 13:27 Comment(0)
M
24

I believe this is what you want:

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

    }
}
Microscopic answered 29/10, 2011 at 13:27 Comment(2)
ItemArray is what I was looking for. Didn't get it via intellisense.Behaviorism
Note that ItemArray is of type Object[], so dc should be typed Object, not DataColumn.Icebound
K
12

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.
        }
     }
}
Kookaburra answered 29/10, 2011 at 13:26 Comment(0)
D
11

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...
 }
Dowery answered 29/10, 2011 at 13:25 Comment(0)
M
7

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

Manwell answered 29/10, 2011 at 13:24 Comment(2)
so you this will stay inside that specific row, thats the part that had me wondering about it?Ency
yeap, you get the row => columns belong to datatable so you can get value of each column in the row.Manwell
D
1

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
}
Dissociate answered 16/9, 2014 at 14:52 Comment(0)
I
1
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];

     }
}
Indiscreet answered 30/1, 2017 at 11:20 Comment(0)
F
-1
string a,b,c;
foreach (DataRow dtRow in dtTable.Rows)
{
    a = dtRow["FirstColumnName"].ToString();
    b = dtRow["SecondColumnName"].ToString();
    c = dtRow["ThirdColumnName"].ToString();
}
Fond answered 28/5 at 6:17 Comment(2)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Stemma
This doesn't answer the question. OP wants to loop through the column values for each data row without hardcoding the set of columns, while your answer hardcodes three columns.Hurlow

© 2022 - 2024 — McMap. All rights reserved.