PDA

Click to See Complete Forum and Search --> : DataTable/Excel


Techno
Apr 23rd, 2007, 08:25 AM
I have a datatable populated with data from an Excel spreadsheet.

What I want to do now is loop through each row and column.

I want to find out on each row and the current column, on if the columnName is the specified start column name. It will then continue on collecting the names of columns until the specified end column name is specified.

this means that we can then go through each row but starting to get data based on the startcolumn and the end column

any ideas?

Negative0
Apr 23rd, 2007, 09:12 PM
Is this what you are trying to do?
DataTable dt;

dt = PopulateMyDataTable();

int iStart = 0;
int iEnd = 0;

// Figure out our starting and ending columns
for (int i = 0; i < dt.Columns.Count;i++ )
{
if (dt.Columns[i].ColumnName == "My Start Column")
iStart = i;
if (dt.Columns[i].ColumnName == "My End Column")
iEnd = i;

}

if (iStart > 0 && iEnd > 0 && iStart < iEnd)
{
// Loop through the rows
for (int j = 0; j < dt.Rows.Count; j++)
{
DataRow objRow = dt.Rows[j];
for (int k = iStart; k <= iEnd; k++)
{
// Read Values from here
}


}
}