Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to update a dynamic datagridview's dataset

Status
Not open for further replies.

azarcomp

Programmer
Joined
Jul 9, 2010
Messages
11
Location
US
I have a dynamic datagridview that brings rows from user tables. Now I need to be able to update the dataset when someone make a change to a cell in in the dataGridView.
Has someone done this before?
 
Yes, it is accomplished quite often in .Net.

Your DataGridView is bound to a DataTable or DataView.

You have a DataAdapter.

Your DataAdapter contains .Select, .Insert, .Update, and .Delete command objects, with mapped parameters.

You call the .Update command of your DataAdapter, passing in a DataTable, which then calls the appropriate command for each row.
 
Thanks River Guy.
I am new at vb.net.
Here is what a developer wrote for me that gets me started.
with the way he is connecting to the ms sql db (below), will I be able to use the above aproach that RiverGuy gave me?
The form has a cbo box that fills all user tables in the database. then when the users selects a table, they hit a green arrow button to display its rows and columns in the dataGridView. The update,delete, and insert of any changes to cells in the grid he has graciously left to me tomake happen. I am sure he is testing me.
RiverGuys aproach sounds to good to be true, but I am sure it will work, I just can't seem to put any dots after the dataGridView1 to see an update,insert, or delete in intellesense.
can someone help?

Here is the code he has started me with:
Imports System.Data.SqlClient

Public Class frmUserTables
Private sqldb As New SqlDataProvider.clsDatabase(MDICatvMain.CommonData.ConnString)
Private Sub frmUserTables_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try
LoadUserTables() '
Catch ex As Exception
MsgBox(ex.Message & " Load User Tables")
End Try
End Sub
Private Sub ToolStripGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripGo.Click
GetTable(ToolStripcboTables.SelectedText.ToString) 'the green arrow button
End Sub
Private Sub GetTable(ByVal strTable As String)
Dim params(0) As SqlParameter

params(0) = New SqlParameter("@table_name", SqlDbType.VarChar)
params(0).Value = strTable

Dim dr As DataSet = sqldb.FillDataset("sp_GetUserTable ", CommandType.StoredProcedure, params)
dgvTables.DataSource = dr
dgvTables.DataMember = "Table"
End Sub
Private Sub LoadUserTables()
'ToolStripcboTables.Rows.Clear()
Dim sSQL As String = "SELECT * FROM vw_UserTables"

Dim objDataReader As SqlDataReader
objDataReader = sqldb.ExecuteReader(sSQL, CommandType.Text)
If objDataReader.HasRows Then

With ToolStripcboTables
.Items.Add("<Select a Table to View>") 'puts at top of drop down
Do While objDataReader.Read()
.Items.Add(objDataReader(0).ToString.Trim) 'loops and populates dropdown
Loop
.SelectedIndex = 0 'sets dropdown index to start at first one which is ....Items.Add("<Select a Table to View>")
End With
End If
End Sub
 
Ok i was wrong I did get an update from intellisense.
example dgvTables.Update .But there is no insert or delete, so does this mean the DataViewGrid is smart enough to know to make any and all changes that where edited in the grid to the table in the database if I put dgvTables.Update in a sub?
 
OK, I know this was obserd to think this would work.
It threw no erros

Try
dgvTables.Update()
Catch ex As Exception
MsgBox(ex.Message & " Update Changes got Hosed")
End Try
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top