I need to populate a table based on the values in columns in the first row of a table. I'm having trouble figuring out how to loop through these columns.

I seem to do ok until I get to the last line here:

Code Snippet

foreach (DataRow row1 in dt_sub2.Rows)
//get # of rows with this substation
string substation = row1[0].ToString();
int itemNo = Convert.ToInt16(row1[1]);
string filterDetail = "Column1 = '" + substation + "' AND Column2 = " + itemNo;
string sort = "Column1, Column2 ASC";

foreach (DataRow rowDetail in ds.tblDetail.Select(filterDetail, sort))
DataRow rowsFound = ds.tblCrossTab.NewRow();

rowsFound["Column1"] = substation;
rowsFound["Column2"] = itemNo;

int colNum = 2;

for (int col = 0; col < 1; col++)
foreach (DataColumn colTab in ds.tblCrossTab.Rows[0]) <----------------this does not work

Any ideas


Re: .NET Framework Data Access and Storage Loop Through DataColumn Fields In Row


ds.tblCrossTab.Rows is an array of DataRows, not DataColumns

you need to do something like

//loop through each column of the first row and show the value
foreach (DataColumn colTab in ds.tblCrossTab.Columns)