When you use datatable. Rows [I]. Delete () to Delete a row and then access the table, the error "cannot access the information of the row deleted" appears. The reason is as follows:
The datatable is required after the Delete () operation. the AccepteChanges () method confirms that the data is completely deleted. Because Delete () only marks the status of the corresponding column as deleted, you can also use datatable. the RejectChanges () rollback cancels the deletion of this row.
Therefore, to completely Delete datarow, you must use both the Delete () and AccepteChanges () methods, or use datatable. rows. the RemoveAt (I) method is directly deleted, where I indicates the row index and a datatable. rows. remove (DataRow dr) deletes a specified row.
However, use datatable. rows. removeAt (I) should note that if the able is used continuously. rows. removeAt (0); datatable. rows. removeAt (1); in this case, the row 0 in the original table is not deleted, but after the row 0 is deleted, the original Row 1 is changed to 0, so the datatable. rows. removeAt (1) actually deletes two rows of the original table.
Therefore, you should use datatable. Rows. RemoveAt (I) with caution. To Delete multiple Rows, you can use Delete () consecutively and then use the AccepteChanges () method to confirm the deletion.
Code Public DataTable GetUserByMonth (string month)
{
DataTable dtUser = GetList ("paymonth> 0 and paymonth <" + month). Tables [0];
For (int I = 0; I <dtUser. Rows. Count; I ++)
{
Decimal Number = (Decimal) dtUser. Rows [I] ["Telenbr"];
Decimal Other = (Decimal) dtUser. Rows [I] ["other"];
Decimal Sum = 0.0 M;
Sum = HFdal. GetQianfei (dtUser. Rows [I] ["paymonth"]. ToString (), month, Number );
If (Other <Sum)
{
DtUser. Rows [I] ["Define3"] = Sum;
DtUser. Rows [I] ["Define4"] = Other-Sum;
}
Else
{
DtUser. Rows [I]. Delete ();
}
}
DtUser. AcceptChanges ();
Return dtUser;
}