Insert & Edit Data in DataGrid

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 3
Reputation: fkatan is an unknown quantity at this point 
Solved Threads: 0
fkatan fkatan is offline Offline
Newbie Poster

Insert & Edit Data in DataGrid

 
1
  #1
Mar 27th, 2007
Hi,
I use databinding.addnew() but when I try to type data in cell an error message apper(DBNULL Exception)....
Pls let me konw: How can I insert or edit data in DataGrid

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: chitrasubbiah is an unknown quantity at this point 
Solved Threads: 0
chitrasubbiah chitrasubbiah is offline Offline
Newbie Poster

Re: Insert & Edit Data in DataGrid

 
0
  #2
Apr 24th, 2007
Originally Posted by fkatan View Post
Hi,
I use databinding.addnew() but when I try to type data in cell an error message apper(DBNULL Exception)....
Pls let me konw: How can I insert or edit data in DataGrid

Thanks
Hi,
To edit,delete and update data u have to write this codes. Drag n drop the appropriate button control needed.This code is in vb.net windows appln.

Imports System.Data.SqlClient
Imports System.Windows.Forms
PublicClass num
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
Dim con As New SqlClient.SqlConnection
Dim com As New SqlCommand
Dim adap As New SqlDataAdapter
Dim ds As New DataSet
con = New SqlConnection("data source=niqotine;initial catalog=record;uid=sa;pwd=mindworks")
con.Open()
com.CommandText = "insert into UserInfo values('" + txtname.Text + "','" + txtid.Text + "','" + cmbcolor.Text + "')"
com.Connection = con
com.ExecuteNonQuery()
adap = New SqlDataAdapter("select * from UserInfo", con)
adap.Fill(ds, "1")
DataGridView1.DataSource = ds.Tables("1")
End Sub
Private Sub num_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con As New SqlConnection
Dim adap As New SqlDataAdapter
Dim ds As New DataSet
con = New SqlConnection("data source=niqotine;initial catalog=record;uid=sa;pwd=mindworks")
con.Open()
adap = New SqlDataAdapter("select * from UserInfo", con)
adap.Fill(ds, "1")
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = ds.Tables("1")
End Sub
Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
txtname.Text = ""
txtid.Text = ""
cmbcolor.Text = ""
End Sub
Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
Dim con As New SqlConnection
Dim com As New SqlCommand
MsgBox("Password Needed")
Me.WindowState = FormWindowState.Minimized
pwd.Show()
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Dim con As New SqlConnection
Dim com As New SqlCommand
Dim ds As New DataSet
Dim adap As New SqlDataAdapter
con = New SqlConnection("data source=niqotine;initial catalog=record;uid=sa;pwd=mindworks")
con.Open()
MsgBox(e.RowIndex)
com.CommandText = "update UserInfo set sname='" & DataGridView1.CurrentRow.Cells(0).Value & "',color='" & DataGridView1.CurrentRow.Cells(2).Value & "' where idno='" & DataGridView1.CurrentRow.Cells(1).Value & "'"
com.Connection = con
MsgBox(e.GetHashCode())
com.ExecuteNonQuery()
adap = New SqlDataAdapter("select * from UserInfo", con)
adap.Fill(ds, "1")
DataGridView1.DataSource = ds.Tables("1")
End Sub
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
Dim con As New SqlConnection
Dim com As New SqlCommand
Dim adap As New SqlDataAdapter
Dim ds As New DataSet
Dim str As String
con = New SqlConnection("data source=niqotine;initial catalog=record;uid=sa;pwd=mindworks")
con.Open()
str = "delete from UserInfo where idno='" & DataGridView1.CurrentRow.Cells(1).Value & "'"
com.CommandText = str
com.Connection = con
com.ExecuteNonQuery()
adap = New SqlDataAdapter("select * from UserInfo", con)
adap.Fill(ds, "1")
DataGridView1.DataSource = ds.Tables("1")
End Sub
End
Class


This code will work .If u have any queries send me reply.

Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 9
Reputation: Dmlang1985 is an unknown quantity at this point 
Solved Threads: 0
Dmlang1985 Dmlang1985 is offline Offline
Newbie Poster

Re: Insert & Edit Data in DataGrid

 
0
  #3
May 6th, 2008
Will the code listed above save all changes to the datagrid or will this only save the current cell?

Or will it work similar to the following:
http://www.daniweb.com/code/snippet836.html
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 9
Reputation: Dmlang1985 is an unknown quantity at this point 
Solved Threads: 0
Dmlang1985 Dmlang1985 is offline Offline
Newbie Poster

Re: Insert & Edit Data in DataGrid

 
0
  #4
May 8th, 2008
Nevermind I answered my own question, and thank you for posting the actual code.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC