I need to find out what the current Row index is from a loop foreach.
foreach (DataRow row in DataTable[0].Rows)
{
}
An exception can occur at any point in the block try/catch, but if I use an incremental counter in the loop foreach, and the exception occurs outside the loop, I get an invalid string, ve moved the pointer one by one.
I know that I can declare DataRowout of scope foreach, but foreachis in the block try/catch. I need to pass the Row index so that I can use it in the statement catch. I have to say mine DataTableis in the class level area.
Is this really the only way to get the current Row index? Or is there a cleaner implementation?
EDIT
So, reflecting on this, I could use intto store the current value of the string and increase this value as follows:
int i = 0;
try
{
foreach (DataRow row in DataTables[0].Rows)
{
i++;
}
}
catch
{
DataRow row = DataTables[0].Rows[i];
}
However, if it foreachdoes not throw an exception, then the value iwill be greater than the actual number of rows in the table. Obviously, I could do it i--;after the loop foreach, but that seems like a really dirty hack.
source
share