15

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
6
  • Why I downvoted this question: meta.stackexchange.com/a/149138/133242
    – Matt Ball
    Mar 18, 2013 at 13:26
  • Did you tried anything? Show your effort first.. And please read FAQ and How to Ask a couple of times.. Mar 18, 2013 at 13:26
  • 2
    -1 Not working is an absolutely horrible description of the problem, whatever it is.
    – lc.
    Mar 18, 2013 at 13:28
  • 4
    Use Rows[0].SetAdded() and Rows[0].SetModified() and Rows[0].Delete() Mar 18, 2013 at 13:30
  • 11
    This question is a great example of overly-aggressive closure of a useful, answerable question. Someone with expertise recognized the issue and provided a response that has likely helped a few thousand people. Comments prompting the asker to provide more detail would have sufficed. Downvoting and voting to close didn't help anyone.
    – Aidan Ryan
    Jul 13, 2015 at 23:05

1 Answer 1

27

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.