Dataset.Copy () does not copy value of new record

I have a dataset with multiple tables. One table in particular has one record. I add another entry to this datatable and use 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 incorrect. There is a new row and has a RowState "Added", but the value is empty (this is a row field). The dataset stores the database with an empty value.

Now I have two lines. Original, and empty. If I change empty to have text and start 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 a space for that row.

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

Edit: The original dataset is modified using the datagridview. If, after entering text in the datagridview cell, I click on another row before starting the copy operation, it works fine. If I don't click on another line, it doesn't work. Before starting the copy operation, I call the datagridview.endit () method. This is still strange because without even clicking on another cell when I debug the code, the original dataset always shows the correct value just before the copy, so I don't understand why the datagridview is not affected at all.

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

0


a source to share


2 answers


The problem turned out to be a problem with the DataGridView control. When the EndEdit () method was called, the DataGridView updated the value in the dataset, but for some reason the RowState remained unchanged. This apparently caused the value to be ignored with the Copy () operation. I believe this is 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 that contains the DataGridView. Then, calling the BindingSource.EndEdit () method ensures that the value is passed to the underlying dataset. This causes the RowState to change correctly to Added or Modified and everything works as it should.



As I said, IMHO I believe this is a bug with the DataGridView control ... especially given the inconsistent behavior. I believe that the steps required to get the data from the bound control through the binding source to the underlying dataset should be better documented because I found many suggested solutions, but each had small differences in the methods called, the objects they called , and the order in which they were called.

0


a source


Have you called the DataSet.AcceptChanges () method? I believe you need the changes to take effect ...



+2


a source







All Articles