Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I explicitly change the RowState of an ADO.Net DataRow? [closed]

Tags:

c#

ado.net

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
like image 530
user1387147 Avatar asked Mar 18 '13 13:03

user1387147


People also ask

How do I change the RowState of a DataRow?

You can use the . SetAdded() method to change the RowState, like this: da. Fill(dt); foreach (DataRow row in dt.

What is the method used to return the DataRow?

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.

Which method of the DataSet returns the records whose RowState is not unchanged?

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.

What is RowState?

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.


1 Answers

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.

like image 58
Mike Perrenoud Avatar answered Sep 20 '22 17:09

Mike Perrenoud