Safely Removing DataRow In ForEach

后端 未结 15 2742
小蘑菇
小蘑菇 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:34

    If you call the delete method you just have to call AcceptChanges() on the table you are modifying, after the foreach loop.

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

    The delete method simply marks the row for deletion.

    http://msdn.microsoft.com/en-us/library/system.data.datarow.delete%28v=VS.90%29.aspx

提交回复
热议问题