views:

592

answers:

2

I have a dataset with multiple tables. One table in particular has one record in it. I am adding another record to this datatable and am using the Dataset.Copy() method to copy the entire dataset object to another instance.

 Dataset2 = DirectCast(Dataset1.Copy(), dsApplication)

However, the new copy of the dataset returned from this method is not correct. The new row is there and has a RowState of "Added", but the value is blank (this is a string field). The dataset saves to the database with the blank value.

Now, I have two rows. The original one, and the blank one. If I change the blank one to have text and initiate the operation again, then the original dataset shows the row as having text, but with a RowState of Unchanged, and the second dataset again has blank for this row.

Does anyone have an explanation for this behavior? Why isn't the value of the second row being copied in the Dataset.Copy() operation? Why isn't the RowState of the second row showing as Modified after adding text?

Edit: The original dataset is being modified via a datagridview. If, after I type the text into the datagridview cell, I click onto another row before initiating the copy operation, it works fine. If I do not click onto another row, then it fails. Before I initiate the copy operation, I call the datagridview.endedit() method. It's still strange though because even without clicking onto another cell, when I debug the code, the original dataset always shows the correct value immediately before the copy operation, so I don't understand why the datagridview is having any affect at all.

Edit #2: While debugging, I noticed that if I change the first value in the gridview, the RowState gets changed to Modified, as expected. However, for any other row, the RowState remains as Unchanged despite the actual value changing.

+1  A: 

Did you call the DataSet.AcceptChanges() method? I believe you need that for the changes to take effect ...

Ron

Ron Savage
Calling the AcceptChanges() method sets the RowState to "Unchanged" and the rows won't be available for the database insert or update commands. This seems to be a bug related to the datagridview, but I haven't figured it out yet.
NYSystemsAnalyst
A: 

The problem turned out to be an issue with the DataGridView control. When calling the EndEdit() method, the DataGridView did update the value in the dataset, but for some reason, the RowState was left as Unchanged. This apparently caused the value to be ignored by the Copy() operation. I believe this to be a bug in the DataGridView because this behavior does not occur if editing the first row in the grid, only others.

To make it work correctly, instead of calling DataGridView.EndEdit(), I have to call the Me.Validate() method on the form containing the DataGridView. Then, a call to the BindingSource.EndEdit() method ensures the value is propagated to the underlying dataset. This results in the RowState being correctly changed to Added or Modified and everything works as it should.

As I said, IMHO I believe this to be a bug with the DataGridView control...especially given the inconsistent behavior. I believe the steps to appropriately get data from the bound control through the binding source to the underlying dataset should be better documented because I found numerous proposed solutions, but each one had slight differences in the methods being called, the objects they were called upon, and the order in which they were called.

NYSystemsAnalyst