Mensaje
De
07/04/2010 11:26:26
 
General information
Foro:
ASP.NET
Category:
ADO.NET
Título:
Miscellaneous
ID de la conversación:
01459084
ID del mensaje:
01459169
Views:
51
You changed your code from .Delete() to .Remove(). Those will have two very different impacts on your DataTable.

The Delete will mark the DataRow as Deleted, but it will remain in the DataTable (your DataView will not see it, as the default for DataViews is to ignore rows marked as Deleted). This way, it's available to be used to delete a record in the database table.

The Remove totally removes the DataRow. It's as if its existence was never there. If you need to send a delete back to your database table you wont' be able to.

Not sure what your intention was, but be sure to take the above into account.

~~Bonnie



>>It's a very simple problem and should be listed in the foreach command reference. You can not remove member of a collection while enumerating it.
>
>I agree.
>
>This works, although it seems bass ackwards:
>
>
>public void ClearDataStore(string DataViewName)
>{
>    DataViewName = DataViewName.ToString().ToLower().Trim();
>
>    DataTable dt = _dsDataStore.Tables[0];
>
>    for(int i = dt.Rows.Count - 1; i >= 0; i--)
>    {
>        DataRow dr = dt.Rows[i];
>
>        string dtItem = dr["DataViewName"].ToString().ToLower().Trim();
>
>        if (dtItem == DataViewName)
>        {
>            dt.Rows.Remove(dr);
>        }
>    }
>
>    _dsDataStore.Tables[0].AcceptChanges();
>}
>
>
>
>
>
>>
>>See Re: MisUnderstanding FOR EACH Thread #1329767 Message #1329799
>>
>>>This seemingly simply process is throwing the error:
>>>
>>>
>>>Collection was modified; enumeration operation might not execute.
>>>
>>>
>>>Here's the code:
>>>
>>>
>>>public void ClearDataStore(string DataViewName)
>>>{
>>>    DataViewName = DataViewName.ToString().ToLower().Trim();
>>>    
>>>    foreach (DataRow Row in _dsDataStore.Tables[0].Rows)
>>>    {
>>>        if (Row["DataViewname"].ToString().ToLower().Trim() == DataViewName)
>>>        {
>>>            Row.Delete();
>>>        }
>>>    }
>>>
>>>    _dsDataStore.Tables[0].AcceptChanges();
>>>}
>>>
>>>
>>>What's the right way to remove rows from a DataSet????
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Responder
Mapa
Ver