Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to Update a database through DataGridView in VB.NET 2008
 

How to Update a database through DataGridView in VB.NET 2008

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.
  • Moved byBob BeaucheminMVPSaturday, September 12, 2009 3:31 PMMoved to a more appropriate group (From:.NET Framework inside SQL Server)
  •  
Rocky Balbao  Saturday, September 12, 2009 12:38 PM
http://www.codeproject.com/KB/vb/DataGridViewEditForm.aspx

Use the code in the above link whichwill provide solution.
Rohini Chavakula  Monday, September 14, 2009 2:12 PM
Refresh the griddata after each change in combobox selection(Combobox_selectedindexchanged) event and then edit the gridcell
Rohini Chavakula  Saturday, September 12, 2009 3:58 PM
pls provide the coding and where to add the coding.

Rocky Balbao  Saturday, September 12, 2009 4:22 PM
http://www.codeproject.com/KB/vb/DataGridViewEditForm.aspx

Use the code in the above link whichwill provide solution.
Rohini Chavakula  Monday, September 14, 2009 2:12 PM

You can use google to search for other answers

Custom Search

More Threads

• .NET data to Blackberry
• Problem with editing cells in dataGridView
• Problem Add Data Source
• Display multiple fields in a DataGridView cell
• why isnt my data saved to the database
• Generic BindingList copy constructor bug
• How do i delete rows in a datagrid?
• disappear a contol from group box using click event
• .net 2.0 programmatically add a row in datagridview
• DatagridView