troubleshooting Question

How do I save updates to a bound DataGridView?

Avatar of tjlemahieu
tjlemahieu asked on
Visual Basic Classic
7 Comments1 Solution819 ViewsLast Modified:
I am new to DataGridViews.  Also, I have used ADO with VB extensively, but using DataAdapters is also new to me.  I am able to populate the DataGridView.  I want the user to be able to edit the grid.  The editing works fine, but how to I commit the edits to the database.  I'm OK with a SAVE button that saves all the changes.

Using the code below, I define the sqlconnection, setup the grid and populate the grid.  

Dim sSqlCnxnDrain As String
Dim SqlCnxnDrain As New SqlConnection
Dim BindingSourceDrain As BindingSource
Dim DataAdapterDrain As New SqlDataAdapter
Dim DataSetDrain As New DataSet
 
Call DefineConnection()
Call SetupGrid()
Call PopulateGrid("287")
Call PopulateAmounts("287")
 
Private Sub DefineConnection()
	'Create Connection object
	sSqlCnxnDrain = "Data Source=LNDDCDA2P\Inst1;initial catalog=drnGIS;integrated security=SSPI;persist security info=False;"
	SqlCnxnDrain = New SqlConnection(sSqlCnxnDrain)
	SqlCnxnDrain.Open()
end sub
 
Private Sub SetupGrid()
 
        DataGridView1.AllowUserToOrderColumns = True
        DataGridView1.AllowUserToDeleteRows = False
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.MultiSelect = False
        DataGridView1.SelectionMode =   DataGridViewSelectionMode.FullRowSelect
        DataGridView1.VirtualMode = False
 
        Dim colSAD As New DataGridViewTextBoxColumn()
        colSAD.DataPropertyName = "SAD"
        colSAD.HeaderText = "SAD"
        colSAD.Name = "SAD"
        colSAD.ReadOnly = True
        colSAD.Visible = False
        DataGridView1.Columns.Add(colSAD)
 
        Dim colPIN As New DataGridViewTextBoxColumn()
        colPIN.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        colPIN.SortMode = DataGridViewColumnSortMode.Automatic
        colPIN.DataPropertyName = "PIN"
        colPIN.HeaderText = "PIN"
        colPIN.Name = "PIN"
        colPIN.ReadOnly = True
        colPIN.Visible = True
        DataGridView1.Columns.Add(colPIN)
 
 End Sub
 
 Private Sub PopulateGrid(ByVal sSAD As String)
 
        Dim sSql As String
 
        'Define Sql String
        sSql = "SELECT DRAIN_APPORTIONMENT_TB.* FROM DRAIN_APPORTIONMENT_TB WHERE SAD='" & sSAD & "' ORDER BY PIN;"
 
 
        'Create Data Adapter
        DataAdapterDrain = New SqlDataAdapter(sSql, SqlCnxnDrain)
 
        'Create a Dataset of the Drain_Apportionment records  
        DataSetDrain = New DataSet
        DataAdapterDrain.Fill(DataSetDrain, "DRAINTABLE")
 
        ' Bind the data table to the data grid
        BindingSourceDrain = New BindingSource(DataSetDrain, "DRAINTABLE")
        DataGridView1.DataSource = BindingSourceDrain
 
End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros