I have a datagridview that is bound to a DataTable within a DataSet. I'm using a TableAdapter to Insert, update and delete any changes that i make. I seem to have a problem when deleting rows. This is my code:

DataTable dtProjects = new DataTable(0);

dtProjects = this.ds.Tables[0];

DataRow drProj = dtProjects.RowsIdea;

Console.WriteLine("Original State: " + drProj.RowState.ToString());


Console.WriteLine("After Delete State: " + drProj.RowState.ToString());

The output from this is:

Original State: Unchanged

After Delete State: Deleted


Original State: Unchanged

After Delete State: Detached

When the rowstate is set to deleted the DataSet.GetChanges() will returns the changes.

However when the rowstate is set to detached this method returns null and therefore does not update the database.

I don't know why the RowState is being set to Detached rather than Deleted. I need the status of the row to be deleted so i can detect changes using the GetChanges(). Is it possible to forcilbly set the RowState to deleted this could solve my problem.

Thanks for your time.


Re: .NET Framework Data Access and Storage DataRow.RowState set to detached instead of deleted

Robert Rossney

The Detached state means "this row isn't in a DataRowCollection anymore." There are two ways to put a DataRow into this state:

  1. Call the Remove() method instead of Delete(). This removes the row from its DataRowCollection.
  2. Call the Delete() method on the row, and then call AcceptChanges().

Ordinarily, all of the deleted DataRows in a table get disposed when something (like the Update method of the TableAdapter) calls AcceptChanges(), so you never see this state. But if you're holding a reference to the DataRow somewhere - like in drProj - you may see it become detached.

Why, in the code you've posted, does drProj become detached sometimes and deleted other times The only reason I can think of is that sometimes you have an event handler on the DataTable that's firing when the Delete() method executes, and other times not, and the event handler's calling AcceptChanges() - the most likely scenario is that someone thought the RowChanged() event would be a good place to make the table adapter update the database.

Re: .NET Framework Data Access and Storage DataRow.RowState set to detached instead of deleted


100% correct thanks very much!

The delete method was triggering the RowValidated event handler which was subsequently causing an update on a different DataTable within my DataSet. This obviously detaches the row from the DataTable after AcceptChanges(), like you said. I've stop this from happening now, and I update all DataTables within my DataSet.

Thanks again.