C# .NET - how to programmatically change the rowstate property of a dataset

Asked By Kapil Desai on 05-Feb-10 05:25 AM

how to programmatically change the rowstate property of a dataset  from Unchanged to Added with around 10000 rows.

i dont want to use loops

Sagar P replied to Kapil Desai on 05-Feb-10 05:35 AM

You cannot directly change RowState, it's a read only property, its value is update depending of what you did with the Row.

You can go for AcceptChanges with some work around.

Kapil Desai replied to Sagar P on 05-Feb-10 05:41 AM
the thing is i am getting the dataset thru a Stored Procedure, and I want tht dataset to be updated into another Table without any changes to dataset.
Web Star replied to Kapil Desai on 05-Feb-10 05:44 AM

It is the current state of the row w.r.t. its relationship to the DataRowCollection.THe value of Rowstate is dependent on the method DataRow.AcceptChanges

so u need to call AcceptChages of dataset for update RowState value.

Huggy Bear replied to Kapil Desai on 05-Feb-10 06:02 AM
You cannot change the RowState programmatically. The only option I could see is, you should create a new table and add the rows from your old table through looping into the new table. This will get the RowState as Added.

Even if you do RejectChanges, it would take you the point where AcceptChanges was performed last.
You have to loop, with the SetAdded() method. - [)ia6l0 iii replied to Kapil Desai on 05-Feb-10 01:24 PM
The SetAdded method (only in 2.0 and above) is a property of the DataRow, and hence you would need to loop. This Datarow method on rows that are unchanged.

The simplest sampe is as below:

myDataSet.EnforceConstraints = false;
for(int counter = 0; counter < myDataSet.Tables["myDataTable"].Rows.Count ; counter ++)
DataRow row =  myDataSet.Tables["myDataTable"].Rows[counter];
myDataSet.EnforceConstraints = true;

Place a System.Diagnostics.Stopwatch around this code, and see how much time it takes.

Hope that helps.