Safely Removing DataRow In ForEach

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

    Sure Magents

    This is how I did it and works fine

    dt = GetStationeryConsolidationDetails(txtRefNo.Text);
    int intRows = dt.Rows.Count;
    int x = 0;
    for (int c = 0; c < intRows; c++)
    {
      if (dt.Rows[c - x]["DQTY"].ToString() == "0")
      {
        dt.Rows[c - x].Delete();
        dt.AcceptChanges();
        x++;        
      }
    }
    

提交回复
热议问题