Hello All,
I am new to windows Forms applications...
I am working in VB.NET windows forms and dealing with DataGridView...
I have populated that DataGridView with the DataSet through wizard...
Please tell me How to Add , Delete and Edit/Update the records of the DATAGridview back to the DataBase...
I am using MS Access as the Database....
Please Please note that I am using Windows Forms application not the WEB application...
Thanks u in Advance...!
5 Replies - 12986 Views - Last Post: 16 February 2013 - 01:37 AM
#1
DataGridview Edit,Update,Insert in Windows VB .nET forms
Posted 18 September 2009 - 10:39 PM
Replies To: DataGridview Edit,Update,Insert in Windows VB .nET forms
#2
Re: DataGridview Edit,Update,Insert in Windows VB .nET forms
Posted 18 September 2009 - 10:46 PM
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Post your code like this:
Thanks.
Post your code like this:
Thanks.
#3
Re: DataGridview Edit,Update,Insert in Windows VB .nET forms
Posted 18 September 2009 - 10:49 PM
PsychoCoder, on 18 Sep, 2009 - 09:46 PM, said:
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
Post your code like this:
Thanks.
Post your code like this:
Thanks.
My Code is as Follws..
Private Sub DataGridView1_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
Me.PostingDetailsTableAdapter.Update(Me.Ds.PostingDetails)
PostingDetailsBindingSource.DataSource = Ds.Tables("PostingDetails")
Me.Ds.AcceptChanges()
Me.Refresh()
End Sub
I am facing the problem of updating deleting and adding the rows in DataBase...
Please please asap....
#4
Re: DataGridview Edit,Update,Insert in Windows VB .nET forms
Posted 18 September 2009 - 11:20 PM
[size=3]
This works for me.
Not Mine got it of the net somewhere.
Public Class Form1
Dim cn As New OleDb.OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim dt As DataTable
Dim i As Integer
Dim constr As String
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
constr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " & Application.StartupPath & "\Student.mdb; Persist Security Info = false"
cn.ConnectionString = constr
Create_Table()
Add_Binding()
End Sub
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
Remove_Binding()
txtRollNo.Clear()
txtFName.Clear()
txtLName.Clear()
txtMobNo.Clear()
txtCity.Clear()
cmd = New OleDbCommand("Select * from StudentInfo", cn)
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
txtRollNo.Text = dr(0)
End While
If txtRollNo.Text = Nothing Then
txtRollNo.Text = 0
End If
cn.Close()
txtRollNo.Text = CInt(txtRollNo.Text) + 1
txtFName.Focus()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
constr = "Insert into StudentInfo values(" & txtRollNo.Text & ",'" & txtFName.Text & "','" & txtLName.Text & "','" & txtMobNo.Text & "','" & txtCity.Text & "')"
cmd = New OleDbCommand(constr, cn)
cn.Open()
i = cmd.ExecuteNonQuery
MessageBox.Show(i & " Record Saved Successfully", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information)
cn.Close()
Create_Table()
Add_Binding()
End Sub
Private Sub Create_Table()
dt = New DataTable("Temp")
cmd = New OleDbCommand("Select * from StudentInfo", cn)
cn.Open()
dr = cmd.ExecuteReader
dt.Clear()
For i = 0 To dr.FieldCount - 1
dt.Columns.Add(dr.GetName(i), dr.GetFieldType(i))
Next
While dr.Read
Dim drw As DataRow = dt.NewRow
For i = 0 To dr.FieldCount - 1
drw(i) = dr(i)
Next
dt.Rows.Add(drw)
End While
dr.Close()
cn.Close()
DataGridView1.DataSource = dt
End Sub
Private Sub Add_Binding()
txtRollNo.DataBindings.Add("Text", dt, dt.Columns(0).Caption)
txtFName.DataBindings.Add("Text", dt, dt.Columns(1).Caption)
txtLName.DataBindings.Add("Text", dt, dt.Columns(2).Caption)
txtMobNo.DataBindings.Add("Text", dt, dt.Columns(3).Caption)
txtCity.DataBindings.Add("Text", dt, dt.Columns(4).Caption)
End Sub
Private Sub Remove_Binding()
txtRollNo.DataBindings.Clear()
txtFName.DataBindings.Clear()
txtLName.DataBindings.Clear()
txtCity.DataBindings.Clear()
txtMobNo.DataBindings.Clear()
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
i = MessageBox.Show("Are you sure you want to update the record", "Confirm Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If i = MsgBoxResult.Yes Then
constr = "Update StudentInfo set FirstName='" & txtFName.Text & "', LastName='" & txtLName.Text & "', MobileNo='" & txtMobNo.Text & "',City='" & txtCity.Text & "' where RollNo=" & txtRollNo.Text
'constr = "Update StudentInfo set FirstName=@fname, LastName=@lname, MobileNo=@mob, City=@city where RollNo=@rollno"
cmd = New OleDbCommand(constr, cn)
'cmd.Parameters.AddWithValue("@fname", txtFName.Text)
'cmd.Parameters.AddWithValue("@lname", txtLName.Text)
'cmd.Parameters.AddWithValue("@mob", txtMobNo.Text)
'cmd.Parameters.AddWithValue("@city", txtCity.Text)
'cmd.Parameters.AddWithValue("@rollno", txtRollNo.Text)
cn.Open()
i = cmd.ExecuteNonQuery()
MessageBox.Show(i & " Record Updated Successfully", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information)
cn.Close()
End If
Remove_Binding()
Create_Table()
Add_Binding()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
i = MessageBox.Show("Delete the record ?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If i = MsgBoxResult.Yes Then
constr = "Delete from StudentInfo where RollNo = " & txtRollNo.Text
cmd = New OleDbCommand(constr, cn)
cn.Open()
i = cmd.ExecuteNonQuery
MessageBox.Show(i & " Record Deleted Successfully", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information)
cn.Close()
Remove_Binding()
Create_Table()
Add_Binding()
End If
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
BindingContext(dt).Position = 0
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
BindingContext(dt).Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
BindingContext(dt).Position += 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
BindingContext(dt).Position = dt.Rows.Count - 1
End Sub
End Class
#5
Re: DataGridview Edit,Update,Insert in Windows VB .nET forms
Posted 19 September 2009 - 07:18 AM
i think using databindings takes care of updates well, use the code given by bambi above to implement the same in your soln
you can specify the direction of binding according to your requirements(one way binding and two way binding)
also, for insertion try using the sql query INSERT INTO <tablename>(columnnames separated by comma) VALUES(values to be inserted corresponding to columnnames specified)
you can specify the direction of binding according to your requirements(one way binding and two way binding)
also, for insertion try using the sql query INSERT INTO <tablename>(columnnames separated by comma) VALUES(values to be inserted corresponding to columnnames specified)
This post has been edited by aks29921: 19 September 2009 - 07:23 AM
#6
Re: DataGridview Edit,Update,Insert in Windows VB .nET forms
Posted 16 February 2013 - 01:37 AM
aks29921, on 19 September 2009 - 07:18 AM, said:
i think using databindings takes care of updates well, use the code given by bambi above to implement the same in your soln
you can specify the direction of binding according to your requirements(one way binding and two way binding)
also, for insertion try using the sql query INSERT INTO <tablename>(columnnames separated by comma) VALUES(values to be inserted corresponding to columnnames specified)
you can specify the direction of binding according to your requirements(one way binding and two way binding)
also, for insertion try using the sql query INSERT INTO <tablename>(columnnames separated by comma) VALUES(values to be inserted corresponding to columnnames specified)
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|