开发者

Remove a row from a DataTable within a DataSet

开发者 https://www.devze.com 2023-04-01 03:57 出处:网络
I have a DataSet with several DataTables inside. I\'m displaying the DataTables in a ListView (I didn\'t know about databinding when I wrote the code). Anyway, I would like to remove rows from the Dat

I have a DataSet with several DataTables inside. I'm displaying the DataTables in a ListView (I didn't know about databinding when I wrote the code). Anyway, I would like to remove rows from the DataTables inside the DataSet.

I have tried this:

foreach (DataRow row in dsData.Tables["Table1"].Rows)
  {
     //find the row that contains the username I'm after
     if (item.SubItems[2].Text == row["LoginName"].ToString())            
       {
         dsData.Tables["Table1"].Rows.Remove(row); //<- main code of interest
       }
  }

I've also tried

dsData.Tables["Table1".Rows.Delete(row);

The problem I'm experiencing is that the when you remove a row I get the exception:

Collection was modified; enumeration operation might not execute.

From what I understand it's because when you remove a row from a ListView the row below it moves up and causes all this trouble. The code itself does what it's supposed t开发者_如何转开发o but it's not nice to see that exception when when you run it.

I was about to rewrite the whole class with a DataGridView but would rather correct that single line if possible :).

EDIT: I'm not even sure a DataGridView would solve the problem anyway.


change the loop to a for loop counting backwards so you don't get that message.

for(int i = dsData.Tables["TAble1"].Rows; i > 0; i--)
{
     if(item.SubItems[2].Text == dsData.Tables["Table1"].Rows[i - 1]["LoginName"].ToString())
         dsData.Tables["Table1"].Rows.Remove(i - 1)
}


Try:

        DataSet dsData = new DataSet();
        List<DataRow> rowsToDelete = new List<DataRow>();

        foreach (DataRow row in dsData.Tables["Table1"].Rows)
        {
            if (item.SubItems[2].Text == row["LoginName"].ToString())
            {
                rowsToDelete.Add(row);
            }
        }

        foreach(DataRow row in rowsToDelete)
        {
            dsData.Tables["Table1"].Rows.Remove(row); 
        }


You need a backwards for loop if you're going to be removing things (explanation of why here)

for (int i = dsData.Tables["Table1"].Rows.Count - 1; i >= 0; i--)
{
    DataRow row = dsData.Tables["Table1"].Rows[i];

    //find the row that contains the username I'm after
    if (item.SubItems[2].Text == row["LoginName"].ToString())            
    {
        dsData.Tables["Table1"].Rows.Remove(row); //<- main code of interest
    }
}


in general you can't remove items from a collection within a loop that is iterating on it what you could do is keep a list of all the row you want to remove (creating it within the loop) and remove all of them OUTSIDE the loop


You can't modify a collection that you are iterating though with a foreach loop, from inside the loop. Do this instead:

for (int i = 0; i < dsData.Tables["Table1"].Rows.Count; i++)
  {
     DataRow row = dsData.Tables["Table1"].Rows[i];
     if (item.SubItems[2].Text == row["LoginName"].ToString())            
       {             
         dsData.Tables["Table1"].Rows.Remove(row); //<- main code of interest
       }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号