SoFunction
Updated on 2025-03-07

Traversing DataSet dataset object instance in C#


//In the following example, use foreach to iterate through all tables in DataSet, traverse all records for each table, and output each value of each row.
foreach (DataTable dt in ) //MyDataSet is a DataSet object that is defined and assigned.
{
foreach (DataRow dr in ) ///Transfer all rows
    {
foreach (DataColumn dc in ) //Transfer all columns
        {
("{0}, {1}, {2}", , , dr[dc]); //Table name, column name, cell data
        }
    }
}

//Transfer the multi rows and columns of the first table in DataSet
foreach(DataRow mDr in [0].Rows )
{
    foreach(DataColumn mDc in [0].Columns)
    {
        (mDr[mDc].ToString());
    }
}