Save changes to datagridview to database using

Posted on 2007-08-01
Last Modified: 2013-11-26
I am working on a windows froms project which i am using a datagridview control on.   I am populating the grid using a datatable which all works find.   I want to be able to save any changes or new rows that the user adds or makes to the rows in the gridview.   How to i save the changes back to the database.   I am using the following code to populate the grid:

Dim sSQL As String = "SELECT * FROM tblLine_Items WHERE Rec_ID = '1' ORDER BY RecordNumber"

        Dim cn As New SqlConnection(sconnstring)
        Dim da As New SqlDataAdapter(sSQL, cn)

        If cn.State = ConnectionState.Closed Then
        End If

        da.Fill(dsLine_Items, "dtLine_Items")

        Me.DataGridView1.DataSource = dsLine_Items.Tables("dtLine_Items")
Question by:Mike_Stevens
    LVL 48

    Expert Comment

    With the update command:

    LVL 48

    Expert Comment

    In your case


    Author Comment

    I want to be able to allow the user to click a button to save the changes back.   If I use you example in  the click event of a button i get a "da not declared" message.   Where do i declare the dataadapter?
    LVL 96

    Expert Comment

    by:Bob Learned
    If you defined the SqlDataAdapter as the module-level, then you could use the Fill method to get the data, and the Update method to write the changes back to the database.

    LVL 48

    Accepted Solution

    You have to declare the dataadapter outside the sub/function that you have like

    Private da As SqlDataAdapter

    Then in your sub/function you do

    da =  New SqlDataAdapter(sSQL, cn)

    Now on the click event you use the update. You have to do the same for the dataset


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Use a Dot Net dll in VBA 56 58
    Third Party On Screen Keyboard, numeric only. 9 34
    C# - SOLID Question 6 35
    vb linq Count Distinct? 1 29
    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now