Safely Removing DataRow In ForEach

后端 未结 15 2759
小蘑菇
小蘑菇 2020-12-14 00:14

I don\'t understand why this code does not work.

foreach (DataRow dataRow in dataTable.Rows)
{
    if (true)
    {
        dataRow.Delete();
    }
}
<         


        
15条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 00:22

    foreach (DataRow dataRow in dataTable.Rows)
    {
        if (true)
        {
            dataRow.Delete();
        }
    }
    
    dataTable.AcceptChanges();
    

    Please Refer the snaps to understatnd the working of it.

    1. Just Deleted but not removed from the DataTable.

    enter image description here

    1. Break point before AcceptChanges() Function. enter image description here
    2. After Eexecuting AcceptChanges() Function. enter image description here

    I Hope this issue resolved now.

提交回复
热议问题