Datatable distinct row Division

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.