How can I explicitly change the RowState from an ADO.Net DataRow?

Given ADO.Net DataRow , how can I change the row of a RowState from Added to Modified or Deleted ?

I tried setting the property directly:

 myDataSet.Tables[0].Rows[0].RowState = DataViewRowState.ModifiedOriginal; 

which leads to the following error message from the compiler:

 error CS0200: Property or indexer 'DataRow.RowState' cannot be assigned to -- it is read only 
+6
source share
1 answer

Although there are methods for setting the RowState property explicitly, such as SetAdded , SetModified and Delete , I think it’s better to understand what exactly happens automatically.

If you need to set RowState to unchanged, then call AcceptChanges on the line and go from there. If you need it to be in an altered state, and it is not currently, there is a good chance that you should have called AcceptChanges in a DataRow or DataTable elsewhere in the logic - this way when you make changes to a line through code, for example :

 row["field"] = "New Value"; 

it changes the RowState to Modified , but now it also has the base state of the Original state because you previously accepted the changes.

My point is that yes, you can install RowState explicitly using several of these methods, but I feel that you just need to work with the ADO.NET interface a little more, as it was intended.

+15
source

All Articles