Accessing deleted rows from a DataTable

Posted by Ken on Stack Overflow See other posts from Stack Overflow or by Ken
Published on 2010-04-26T14:19:25Z Indexed on 2010/04/26 14:23 UTC
Read the original article Hit count: 414

Filed under:
|
|
|

Hello:

I have a parent WinForm that has a MyDataTable _dt as a member. The MyDataTable type was created in the "typed dataset" designer tool in Visual Studio 2005 (MyDataTable inherits from DataTable) _dt gets populated from a db via ADO.NET. Based on changes from user interaction in the form, I delete a row from the table like so:

_dt.FindBySomeKey(_someKey).Delete();

Later on, _dt is passed by value to a dialog form. From there, I need to scan through all the rows to build a string:

           foreach (myDataTableRow row in _dt)
            {
                sbFilter.Append("'" + row.info + "',");
            }

The problem is that upon doing this after a delete, the following exception is thrown: DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row.

The work around that I am currently using (which feels like a hack) is the following:

           foreach (myDataTableRow  row in _dt)
            {
                if (row.RowState != DataRowState.Deleted &&
                    row.RowState != DataRowState.Detached)
                {
                    sbFilter.Append("'" + row.info + "',");
                }
            }

My question: Is this the proper way to do this? Why would the foreach loop access rows that have been tagged via the Delete() method??

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#2.0