Datagridview only updates database after changing row selection...

thefunnyman

Member
Joined
Oct 7, 2009
Messages
5
Programming Experience
3-5
I have a bound datagrid that contains textboxes and comboboxes. I am having problems getting my changes to consistently update the database behind the scenes. I have an EndEdit event that fires everytime it needs to and executes the code to update the database. However, the update commands only seem to care about the first row, with some caveats.

Let me see if I can explain this. The first row updates just fine. I enter text or select a value from the combobox and the endedit event fires and updates the database. If I move to the next row, and do the same thing, the endedit event fires and the code to update the database executes. but, my changes aren't actually saved. I catch no exceptions as a result of this. Here's the kicker. If I change row 2 and then select row 3, then my changes to row 2 are reflected in the database. Basically, I have to change rows when updating any rows other than row 1 in order for my changes to be saved to the database. Below is the relevant code that updates the db. This code is confirmed to fire every time i update a cell anywhere in the datagridview.

VB.NET:
dgv.EndEdit()
dgv.BindingContext(ds.Tables("dbTable")).EndCurrentEdit()
adapter.Update(ds.Tables("dbTable"))

Any help would be greatly appreciated. Getting close to the project deadline.

Thanks,

thefunnyman
 
Read the DW2 link in my signature, section Creating a Simple Data App

Does that datagridview (when you do it) also exhibit the problem?
I suspect not

The problem is likely to be that youre not calling endedit at the right time, or on the right thing. EndEdit should be called before Update(), not just on the DGV, but the underlying bindingsource too (if youre using them - follow the tutorial)

Update() shouldnt be called every time your user moves a row selection in a datagrid; it sidesteps the intentions of local caching and editing of data. An analogy would be saving a word document after every jeypress; you just don't do it for performance reasons
 
Read the DW2 link in my signature, section Creating a Simple Data App

Does that datagridview (when you do it) also exhibit the problem?
I suspect not

The problem is likely to be that youre not calling endedit at the right time, or on the right thing. EndEdit should be called before Update(), not just on the DGV, but the underlying bindingsource too (if youre using them - follow the tutorial)

Update() shouldnt be called every time your user moves a row selection in a datagrid; it sidesteps the intentions of local caching and editing of data. An analogy would be saving a word document after every jeypress; you just don't do it for performance reasons

Thank you for your reply. The endedit event I have registered is fired whenever the user completes editing an individual cell. The update codes supplied in my first post is ONLY executed in the endedit event. Like I said, it works as intended whenever i edit data on the first row, but not for other rows in the datagridview.

I will review your suggestions tomorrow whenever i am in front of my workstation. I might also explore not saving everytime a user edits a cell in the datagridview, but instead forcing the user to press a save button and see if the inconsistency persists. I'm certain that it's something I'm missing or not fully understanding. I'm not really used to having/using controls in my applications that are supposed to auto-update.

Thanks again. I will post back after exploring your suggestions further.

thefunnyman
 
VB.NET:
bindingSource.EndEdit()

That fixed my problem. Thanks for the tutorial link. It helped me understand a lot more about what I'm trying to accomplish.
 
I have a bound datagrid that contains textboxes and comboboxes. I am having problems getting my changes to consistently update the database behind the scenes. I have an EndEdit event that fires everytime it needs to and executes the code to update the database. However, the update commands only seem to care about the first row, with some caveats.

Let me see if I can explain this. The first row updates just fine. I enter text or select a value from the combobox and the endedit event fires and updates the database. If I move to the next row, and do the same thing, the endedit event fires and the code to update the database executes. but, my changes aren't actually saved. I catch no exceptions as a result of this. Here's the kicker. If I change row 2 and then select row 3, then my changes to row 2 are reflected in the database. Basically, I have to change rows when updating any rows other than row 1 in order for my changes to be saved to the database. Below is the relevant code that updates the db. This code is confirmed to fire every time i update a cell anywhere in the datagridview.

VB.NET:
dgv.EndEdit()
dgv.BindingContext(ds.Tables("dbTable")).EndCurrentEdit()
adapter.Update(ds.Tables("dbTable"))

Any help would be greatly appreciated. Getting close to the project deadline.

Thanks,

thefunnyman


Go to .Net Solution: DataGridView Solutions, to see solution for DataGridView.RowEndEdit Event.

I faced similar problem, there is no RowEndEdit event in Microsoft's .Net Framework.
I didn't find any standard solution anywhere. I have made and used this trick.

I have posted solution on .Net Soution: DataGridView.RowEndEdit in VB and C#. I hope it may be helpful to you.
 

Latest posts

Back
Top