Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I'm trying to read a datagridview and post the changes back to the SQL database. Does anyone have some straightforward code that will work. I'm a newbie in VB.net.

[confused]
Posted
Updated 6-Jun-22 6:04am

Hi Roger,
When people ask for "Straightforward code" it's kind of impossible to respond. All code is tailored to an individual need and no one is going to have EXACTLY the code you want. (Nor will they create it for you if you are not willing to put some effort into it yourself.) The best way to start a project like this is to read some articles about what you are doing. You can search codeproject for articles, like this.[^]. Then as you implement code you've found in the article, if you run into problems post your code and error messages and a description of what your program is doing and what you want it to do. Then we can help you. Hope this gives you a good place to start.
 
Share this answer
 
I have a access connection with textbox as data feeder to database change it to SQL if u want.
The code is:
VB
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form2
    Dim conaccess As New OleDbConnection
    Dim conreader As OleDbDataReader
    Dim concmd As New OleDbCommand


    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        DataGridView1.EditMode = False
        conaccess.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=d:\vijay.mdb"
        conaccess.Open()
        loadGrid()
    End Sub

    Private Sub loadGrid()
        Dim access As String
        access = "select * from vijay"
        Dim DataTab As New DataTable
        Dim DataAdap As New OleDbDataAdapter(access, conaccess)
        DataAdap.Fill(DataTab)
        DataGridView1.DataSource = DataTab
    End Sub

    Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click

        Dim no As String
        no = "select Max(ID) from vijay"
        Dim concmd As New OleDbCommand(no, conaccess)
        conreader = concmd.ExecuteReader
        If (conreader.Read) Then
            If (IsDBNull(conreader(0))) Then
                id_txt.Text = "1"
            Else
                id_txt.Text = conreader(0) + 1
            End If
            name_txt.Clear()
            branch_txt.Clear()
            age_txt.Clear()
            class_txt.Clear()
            gen_txt.Clear()
        End If
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim i As Integer

        i = DataGridView1.CurrentRow.Index
        Try
            id_txt.Text = DataGridView1.Item(0, i).Value
            name_txt.Text = DataGridView1.Item(1, i).Value
            class_txt.Text = DataGridView1.Item(2, i).Value
            gen_txt.Text = DataGridView1.Item(3, i).Value
            branch_txt.Text = DataGridView1.Item(4, i).Value
            age_txt.Text = DataGridView1.Item(5, i).Value
        Catch ex As Exception

        End Try
        
    End Sub


    Private Sub del_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles del_btn.Click
        Dim delcmd As New OleDbCommand("delete from vijay where id=" & id_txt.Text & " ", conaccess)
        delcmd.ExecuteNonQuery()
        MsgBox("Record is deleted")
        loadGrid()
        id_txt.Clear()
        name_txt.Clear()
        branch_txt.Clear()
        age_txt.Clear()
        class_txt.Clear()
        gen_txt.Clear()
    End Sub

    Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
        Dim access As String = String.Format("INSERT INTO vijay (Name,Class,Branch,Gender,Age) VALUES('{0}','{1}','{2}','{3}','{4}')", name_txt.Text, class_txt.Text, branch_txt.Text, gen_txt.Text, age_txt.Text)
        concmd.Connection = conaccess
        concmd.CommandText = access
        concmd.ExecuteNonQuery()
        MsgBox("Record Successfully Saved")
        loadGrid()
        id_txt.Clear()
        name_txt.Clear()
        branch_txt.Clear()
        age_txt.Clear()
        class_txt.Clear()
        gen_txt.Clear()
    End Sub

    Private Sub up_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up_btn.Click
        Dim access As String
        access = "UPDATE vijay SET Name = '" & name_txt.Text & "', Age = '" & age_txt.Text & "', Gender ='" & gen_txt.Text & "' , Branch ='" & branch_txt.Text & "' , Class = '" & class_txt.Text & "' where id=" & id_txt.Text & ""
        Dim cmd As New OleDbCommand(access, conaccess)
        cmd.ExecuteNonQuery()
        loadGrid()
        id_txt.Clear()
        name_txt.Clear()
        branch_txt.Clear()
        age_txt.Clear()
        class_txt.Clear()
        gen_txt.Clear()

    End Sub
End Class
 
Share this answer
 
v2
Comments
Sandeep Mewara 19-Mar-11 2:02am    
Always wrap your code in PRE tags. It makes the question readable as it formats it.
Member 8600075 17-Feb-15 3:18am    
After Load data from DBase with Searching Key (Transaction No), how if I want to edit data in DgView with multiple Row and than I save againt after change Value or edit in DgV

Sorry with my english
I have made a project in visual basic 2010 and sql server, i want to update the record in party master in another form , where when i click on the update button the another update form will open and there in the dtagrid all the records of data table will present, on that form there is three button , update, delete, cancel, i just want that when i click on the update button after editing the data row the data save to the datagrid and same to data table in sql server.

the update form code is below.


Imports System.Data.SqlClient
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms.DataGridView
Public Class frmupdate
Inherits System.Windows.Forms.Form
Dim cnn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\documents\visual studio 2010\Projects\VRINDAVAN REALTECH\VRINDAVAN REALTECH\vrindavan.mdf;Integrated Security=True;User Instance=True ")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim ds As New DataSet()




Private Sub frmupdate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'TODO: This line of code loads data into the 'VrindavanDataSet3.p_master' table. You can move, or remove it, as needed.
Me.P_masterTableAdapter.Fill(Me.VrindavanDataSet3.p_master)
Dim sql As String
Try
cnn.Open()
Dim da As SqlDataAdapter
Dim ds As New DataSet()
sql = "Select * from p_master;"
da = New SqlDataAdapter(sql, cnn)

da.Fill(ds, "p_master")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "p_master"
DataGridView1.DataSource = ds.DefaultViewManager

cnn.Close()

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try

End Sub

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
ds.Tables("p_master").AcceptChanges()
da.Update(ds, "p_master")
Dim item As New DataGridViewRow
DataGridView1.AllowUserToAddRows = True
item.CreateCells(DataGridView1)
MessageBox.Show("Record updated successfully")


End Sub
End Class


Please give me the solution, it's very urgent.
 
Share this answer
 
Private Sub frmupdate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900