String strquerystring = "select * From gx_xtczy ";
Sqldataadapter sqlda = new sqldataadapter (strquerystring, sqlconn );
Dataset DS = new dataset ();
Sqlda. Fill (DS, "dt1 ");
Datatable dt = Ds. Tables ["dt1"];
For (Int J = 0; j <3; j ++)
{
Int x = DT. Rows. Count-1;
For (INT I = 0; I <X; I ++)
{
If (I % 2 = 1)
{
Try
{
DT. Rows [I]. Delete ();
Break;
}
Catch (exception ex)
{}
}
}
}
CodeAs shown above, debug finds that every time an exception is reported in DT. Rows [I]. Delete:
Itemarray "DT. Rows [I]. itemarray" triggers an exception of the "system. Data. deletedrowinaccessibleexception" type object [] {system. Data. deletedrowinaccessibleexception}
Base exception:
{"The information of the row cannot be accessed through the deleted row. "} System. systemexception {system. Data. deletedrowinaccessibleexception}
Why ???
The same is true for rows. removeat (I .......
The code for deleting the object is as follows. The removeat method is changed this time:
Sqldataadapter sqlda = new sqldataadapter (strquerystring, sqlconn );
Dataset DS = new dataset ();
Sqlda. Fill (DS, "dt1 ");
Datatable dt = Ds. Tables ["dt1"];
For (Int J = 0; j <4; j ++)
{
Int x = DT. Rows. Count-1;
For (INT I = x; I> = 0; I --)
{
If (I % 2 = 1)
{
Try
{
// DT. Rows [I]. Delete ();
DT. Rows. removeat (I );
Break;
}
Catch (exception ex)
{}
}
}
}
The code of the delete method cannot reach the goal ,.
Use the removeat or remove method.
If you want to use the delete method, add a State judgment before the delete method .....
Datarowcollection. Remove
When a row is removed, all data in the row is lost. You can also call the delete method of the datarow class to mark a row for removal. Calling remove is equivalent to calling Delete before calling acceptchanges.