C# Getting column name from dataset
Nov 2, 2018
There are many ways we can do the same, here I am sharing how you can do it using foreach loop. Below sample code loop through Dataset and get column the names.
DataSet dsresult = GetResultFromDBQuery(); //Query to db for getting records.
foreach (DataColumn column in dsresult.Tables[0].Columns)
{
Console.WriteLine(column.ColumnName);
}
If you want to get column and row values you can do somethign like below:
DataSet tableDataSet = GetResultFromDBQuery(); //Query to db for getting records.
foreach (DataRow dr in tableDataSet.Rows)
{
for (int i = 0; i < tableDataSet.Columns.Count; i++)
{
Console.WriteLine(tableDataSet.Columns[i].ColumnName, dr[tableDataSet.Columns[i].ColumnName].ToString());
}
}
Hope this will help someone!!!