We help IT Professionals succeed at work.

Updating Datagrid with VB .NET VS 2003

Last Modified: 2008-04-24
I have a VB.Net app that displays some data in a datagrid. Everything works great except updating the database. It seems that the datagrid wont update the dataset until the user selects another row in the grid. That's fine and dandy IF the user moves to the next row but what if the user updates a field then clicks out of the grid on a different control like a command button or menu item. The changes aren't updated in the dataset which means no changes in the database. So my question is how can I force the datagrid to update the dataset when the user moves out of the grid?

Here is the code that I am using to update the database from the dataset.
Private Sub dgTB_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgTB.CurrentCellChanged
        If dsFAACA.HasChanges Then
            Dim cb As New SqlCommandBuilder(daAdapter)
            daAdapter.Update(dsFAACA, "TBImport")
        End If

    End Sub
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)


Thanks but I've tried that and it doesn't work because the dataset doesn't recieve any changes from the datagrid until the user moves to another row. That's the hole problem. If the user changes one cell then moves out of the datagrid without moving to another row no changes are made in the dataset so when I call the "haschanges" event there is nothing to update. I'm looking for a way to force the change to be reflected in the dataset. I've found several post on the web with the same issue but no solutions. Well at least not for Vs 2003. I'm sure the experts here can find a way to do this.
How about dgTB.LostFocus?

It's partly a design issue. Do you want to save changes every time the user leaves the form?

Are you saving records to a DataTable/DataSet or directly to the underlying database?

Some applications have autosave functionality. A timer runs and saves the data every x minutes.

More questions than answers, I'm afraid.



Thanks guys. It turned out to be my mistake and nothing to do with the datagrid or dataset. cheddar73 was correct about putting it in the Leave event handler. I thought I already did that but I put it in the LostFocus event and not the Leave event. Everything is working like a charm now and I apoligize for wasting everyone's time on this.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.