How to loop through DataRow[] to get the column name and value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sri13
    New Member
    • Sep 2010
    • 11

    How to loop through DataRow[] to get the column name and value

    Hi
    I have a DataTable and get selected values out of it in a DataRow collection. I have the code that I want to make it run. Not sure if this is the correct approach to store column names and value in memory for later use?
    Code:
    DataTable dt = ds.Tables["Employee"];
    DataRow[] foundEmpRows = dt.Select("Dept = 10");
    
    NameValueCollection nvEmp = new NameValueCollection();
    foreach (DataRow dr in foundEmpRows)
    {
      [B]foreach (DataColumn dc in foundEmpRows)[/B]
      {
        nvEmp.Add(dc.ColumnName, dr[dc]);
      }
    }
    The statement in BOLD is giving error. How to access DataColumn from DataRow[] array?
  • Sri13
    New Member
    • Sep 2010
    • 11

    #2
    I was able to resolve it myself :)
    I updated the inner loop to:
    Code:
    foreach (DataColumn dc in dr.Table.Columns) 
    { 
      nvEmp.Add(dc.ColumnName.ToString(), dr[dc].ToString()); 
    }

    Comment

    Working...