5 Replies - 20499 Views - Last Post: 16 February 2013 - 01:37 AM Rate Topic: -----

#1 kbdotnet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-September 09

DataGridview Edit,Update,Insert in Windows VB .nET forms

Post icon  Posted 18 September 2009 - 10:39 PM

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...!

Is This A Good Question/Topic? 0
  • +

Replies To: DataGridview Edit,Update,Insert in Windows VB .nET forms

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

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: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 kbdotnet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-September 09

Re: DataGridview Edit,Update,Insert in Windows VB .nET forms

Posted 18 September 2009 - 10:49 PM

View PostPsychoCoder, 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: :code:

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....
Was This Post Helpful? 0
  • +
  • -

#4 bambi18  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 02-November 08

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



Was This Post Helpful? 1
  • +
  • -

#5 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

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)

This post has been edited by aks29921: 19 September 2009 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 ullas1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-February 13

Re: DataGridview Edit,Update,Insert in Windows VB .nET forms

Posted 16 February 2013 - 01:37 AM

View Postaks29921, 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)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1