4

I have a data set:

DataSet ds= _ExecuteQuery(contentCmd, CommandType.Text, contentParams);

// Column name
foreach (DataColumn column in ds.Tables[0].Columns)
{
  Console.WriteLine(column.ColumnName);
}

// Rows value
foreach (DataTable dt in ds.Tables){
     foreach (DataRow dr in dt.Rows){
        Console.Writeline(dr.Table.Rows[0]["name"].ToString());
     }
}

How can I retrieve rows value and column name of the row?

List<columnName, name>;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alvin
  • 8,219
  • 25
  • 96
  • 177
  • Possible duplicate of [DataColumn Name from DataRow (not DataTable)](http://stackoverflow.com/questions/12823371/datacolumn-name-from-datarow-not-datatable) Doesn't they look alike? – Prashanth Benny Feb 20 '17 at 07:10

1 Answers1

5

I assume that you're looking for something like this:

        Dictionary<string, string> vals = new Dictionary<string, string>();

        foreach (DataRow dr in dt.Rows)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                vals.Add(dt.Columns[i].ColumnName, dr[dt.Columns[i].ColumnName].ToString());
            }
        }

Depends on your dataset, but this gives a full dictionary of each column name and corresponding value

Dieter B
  • 1,142
  • 11
  • 20