Hi,
I am Using VB.NET 2008 and SQL SERVER 2005.
I have a DATAGRIDVIEW and COMBO BOX in a Form. On selecting the items in COMBOBOX which is from database containing Unique Id, the values related to the Id is displayed in the DATAGRIDVIEW. I want to edit the Items in the cell in datagridview and update it to the database. My TABLE name is "salesentries" AND database name is "salesdatas" It contains 20 columns with a Primary Key of "Id".
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Data.Common
Public Class ViewForm
Dim sda As SqlDataAdapter
Dim ds As DataSet
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
Dim cn As New SqlConnection("Data Source=RB-PC\SQLEXPRESS;Initial Catalog=salesdatas;Integrated Security=True;Pooling=False")
cn.Open()
Dim updcmd As SqlCommand = New SqlCommand("Select * from salesentries where id = '" + ComboBox1.Text + "' ", cn)
sda = New SqlDataAdapter(updcmd)
Dim ds As New DataSet
sda.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub ViewForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim cn As New SqlConnection("Data Source=RB-PC\SQLEXPRESS;Initial Catalog=salesdatas;Integrated Security=True;Pooling=False")
cn.Open()
Dim updcmd As SqlCommand
updcmd = New SqlCommand("Select Id from salesentries", cn)
sda = New SqlDataAdapter(updcmd)
ds = New DataSet("salesentries")
' sda.TableMappings.Add("Table", "salesentries")
sda.Fill(ds, "salesentries")
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "salesentries.Id"
ComboBox1.ValueMember = "Id"
Catch EX As Exception
MsgBox(EX.ToString)
End Try
End Sub
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Dim cb As New SqlCommandBuilder(sda)
Me.Validate()
sda.Update(ds, "salesentries")
End Sub
Pls verify the code and reply back. There is no Errors are occuring while execution. For DataEntry I have a separate form. On editing the cells in the DataGridView the changes are not occuring when i change the Id in ComboBox or if I close and execute it again.