943,965 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 94918
  • VB.NET RSS
Mar 27th, 2007
1

Insert & Edit Data in DataGrid

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fkatan is offline Offline
3 posts
since Mar 2007
Apr 24th, 2007
1

Re: Insert & Edit Data in DataGrid

Click to Expand / Collapse  Quote originally posted by fkatan ...
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.

Reputation Points: 10
Solved Threads: 0
Newbie Poster
chitrasubbiah is offline Offline
1 posts
since Apr 2007
May 6th, 2008
0

Re: Insert & Edit Data in DataGrid

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dmlang1985 is offline Offline
11 posts
since May 2008
May 8th, 2008
0

Re: Insert & Edit Data in DataGrid

Nevermind I answered my own question, and thank you for posting the actual code.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dmlang1985 is offline Offline
11 posts
since May 2008
Feb 5th, 2010
0

Thanx a lot

I was trying this since last two days but was unable to solve it.
Finally i got it thanx.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
meet_modiani is offline Offline
1 posts
since Feb 2010
Jun 30th, 2010
0

Hi

hi could any one give some example to how add data in Combobox in grid view and how to edit it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Oviyaa is offline Offline
1 posts
since Jun 2010
Jul 16th, 2010
0
Re: Insert & Edit Data in DataGrid
& DataGridView1.CurrentRow.Cells(0).Value & this part will show the error
as below


Error 1 Operator '&' cannot be applied to operands of type 'string' and 'object' C:\Documents and Settings\gts\My Documents\Visual Studio 2005\First\WindowsApplication3\Form1.cs 128 35 WindowsApplication3





please help to how to solve this problem
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sivakumarsnr is offline Offline
1 posts
since Jul 2010
Jul 29th, 2010
0

Datagridview Tutorial

Go through the following tutorial , it will help u.

http://vb.net-informations.com/datag...w_tutorial.htm

jerry.


& DataGridView1.CurrentRow.Cells(0).Value & this part will show the error
as below


Error 1 Operator '&' cannot be applied to operands of type 'string' and 'object' C:\Documents and Settings\gts\My Documents\Visual Studio 2005\First\WindowsApplication3\Form1.cs 128 35 WindowsApplication3





please help to how to solve this problem
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jerry32uk is offline Offline
14 posts
since Apr 2008
Mar 2nd, 2011
0

Datagridview

this code is woking. but when i hit the btnsave its just populating the whole data in twise. after closing and repoen it is showing the saved data correctly.y?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
VB Prash is offline Offline
1 posts
since Mar 2011
24 Days Ago
0
Re: Insert & Edit Data in DataGrid
The item "obj\Debug\Welcome.Addmarks.resources" was specified more than once in the "Resources" parameter. Duplicate items are not supported by the "Resources" parameter.


will u pls solve this? i can't able to understand this.....plsss
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ReshmaRajan is offline Offline
3 posts
since Jan 2012
Message:
Previous Thread in VB.NET Forum Timeline: how to ignore unfound GetElementById ?
Next Thread in VB.NET Forum Timeline: WPF





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC