Link to home
Start Free TrialLog in
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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Try following in btnSave

da.Update(dt)
Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

But this will update all records isnt it.
But how to update only that record which has been changed.
da.update(ds)
can' i write my own update query
It will only update modified records.
But i have to do it on btnsave.
Or no need of that would it automatically save.
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
Its not working because

da.Update(dt)

needs to be called AFTER user has made changes.
so if i call on btnsave.
then again connection needs to be established. is it so
or on which event of gridview it should be called.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial