Given an ADO.Net DataRow
, how I can change the row's RowState from Added
to Modified
or Deleted
?
I tried setting the property directly:
myDataSet.Tables[0].Rows[0].RowState = DataViewRowState.ModifiedOriginal;
resulting in the following error message from the compiler:
error CS0200: Property or indexer 'DataRow.RowState' cannot be assigned to -- it is read only
You can use the . SetAdded() method to change the RowState, like this: da. Fill(dt); foreach (DataRow row in dt.
You use DataTable's NewRow method to return a DataRow object of data table, add values to the data row and add a row to the data Table again by using DataRowCollection's Add method.
The GetChanges method is used to produce a second DataSet object that contains only the changes introduced into the original. Use the rowStates argument to specify the type of changes the new object should include. This returned copy is designed to be merged back in to this original DataSet.
The RowState property indicates whether the row belongs to a table, and if it does, whether it's newly inserted, modified, deleted, or unchanged since it was loaded.
Though there are methods for setting the RowState
property explicitly, like SetAdded
, SetModified
and Delete
, I think it's better to understand what exactly happens automatically.
If you need to set the RowState
to unchanged, then call AcceptChanges
on the row and go from there. If you need it to be in a modified state, and it's not currently, there's a good chance you should have called AcceptChanges
on the DataRow
or DataTable
somewhere else in the logic - this way when you make modifications to the row via code like:
row["field"] = "New Value";
it changes the RowState
to Modified
but now it also has a baseline, the Original
row state, because you had accepted the changes previously.
My point here is that, yes, you can set the RowState
explicitly with a few of those methods, but I feel like you may just need to work with the ADO.NET interface a bit more like it was intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With