C datatable cannot access the row's information handling method through a deleted row

  • 2020-05-07 20:21:01
  • OfStack

for the following reasons:

After Delete(), the datatable.AccepteChanges () method is required to confirm complete deletion, because Delete() simply marks the status of the corresponding column as deletion,
The row can also be undeleted by rolling back datatable.RejectChanges ().

If you want to delete datarow completely, you need to use both the Delete() and AccepteChanges() methods, or use the datatable.Rows.RemoveAt (i) method directly.
Where i represents a row index, another is datatable.Rows.Remove (DataRow dr) to delete the specified row.

However, when using datatable.Rows.RemoveAt (i), be careful if you use datatable.Rows.RemoveAt (0) continuously; datatable. Rows. RemoveAt (1);
Instead of deleting row 0, 1 in the original table, row 1 becomes row 0 when row 0 is deleted, so datatable.Rows.RemoveAt (1) actually deleted two rows in the original table.
Therefore, datatable.Rows.RemoveAt (i) should be used with caution. If you want to delete multiple lines, you can continue to use Delete(), and then use AccepteChanges() method to confirm the deletion.

Example of solution:

 
List<string> lst = new List<string>(); 
for (int i = 0; i < _Table.Rows.Count; i++) 
{ 
if (_Table.Rows[i].RowState!=DataRowState.Deleted) 
{ 
lst.Add(_Table.Rows[i]["I_SL"].ToString() == "0" ? "false" : "true"); 
} 
} 

Related articles: