Avatar of searchsanjaysharma
searchsanjaysharma
 asked on

How to Edit DataGridview1 in vb.Net Windows Form and save it to database.

I have the button btnsave, and btnfind and txtid.Text.

The users enters the id in txtid, clicks on btnfind and the respective record is binded to the DataGridView1

The users clicks on any cell and modifies the existing values.

Now i have btnsave, when the user clicks on the this, the new values of that Datagridview1 s should be stored in database.

1. How to do this. The current code is as follows.


Imports System.Data
Imports System.Data.SqlClient
Public Class Form3
    Dim con As SqlConnection
    Dim dt As DataTable
    Dim da As New SqlDataAdapter
    Dim s As String

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        'call update procedure where id= txtid.Text
    End Sub

    Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        Try
            DataGridView1.Height = Me.Height
            DataGridView1.Width = Me.Width
            con = New SqlConnection("Data Source=172.16.3.17;Initial Catalog=testadms;User Id=sa;Password=admin#cgc;")
            If (con.State = ConnectionState.Closed) Then
                con.Open()
            End If
            s = "SELECT id,firstname,lastname from mstusers where id=" & Convert.ToInt32(txtid.Text.Trim())
            da = New SqlDataAdapter(s, con)
            dt = New DataTable()
            da.Fill(dt)
            DataGridView1.DataSource = dt
        Catch ex As Exception
            MsgBox("Error occured " & ex.ToString())
        Finally
            con.Close()
        End Try
    End Sub
End Class
Visual Basic.NET

Avatar of undefined
Last Comment
searchsanjaysharma

8/22/2022 - Mon
Dirk Haest

Nasir Razzaq

Try following in btnSave

da.Update(dt)
searchsanjaysharma

ASKER
But this will update all records isnt it.
But how to update only that record which has been changed.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
searchsanjaysharma

ASKER
da.update(ds)
can' i write my own update query
Nasir Razzaq

It will only update modified records.
searchsanjaysharma

ASKER
But i have to do it on btnsave.
Or no need of that would it automatically save.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
searchsanjaysharma

ASKER
Not w0oking


 Private Sub btnfind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfind.Click
        Try
            DataGridView1.Height = Me.Height
            DataGridView1.Width = Me.Width
            con = New SqlConnection("Data Source=JAI-PC\SQLEXPRESS;Initial Catalog=testadms;Integrated Security=true;")
            If (con.State = ConnectionState.Closed) Then
                con.Open()
            End If
            s = "SELECT state,district from statedistrict"
            da = New SqlDataAdapter(s, con)
            dt = New DataTable()
            da.Fill(dt)
            DataGridView1.DataSource = dt
            da.Update(dt)

        Catch ex As Exception
            MsgBox("Error occured " & ex.ToString())
        Finally
            con.Close()

        End Try
    End Sub
Nasir Razzaq

Its not working because

da.Update(dt)

needs to be called AFTER user has made changes.
searchsanjaysharma

ASKER
so if i call on btnsave.
then again connection needs to be established. is it so
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
searchsanjaysharma

ASKER
or on which event of gridview it should be called.
ASKER CERTIFIED SOLUTION
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
searchsanjaysharma

ASKER
ok