Hi,
I have a winform with a datagrid who is filled from a database, this
is working. Now I had place
a "save" button on my form. This is where I'm lost, the main question is
how can I fill my database
with the new data from the datagrid?
Any code snipper, url, hints will be welcome.
Thanks for your time
Sylvain Bissonnette
Cerebrus - 22 Feb 2006 14:14 GMT
Hi,
If you're loading data into the DataGrid using a DataAdapter and DataSet,
then just call the DataAdapter.Update(myDataSet) method. You're Dataset is
automatically updated with changes that occur in your Datagrid, and these
changes are then committed to the Database using the Update method.
Regards,
Cerebrus.
> Hi,
>
[quoted text clipped - 8 lines]
> Thanks for your time
> Sylvain Bissonnette
JSantora - 23 Feb 2006 16:46 GMT
Here you go, hope this helps...
Private Sub frmStaff_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
myQuery = "f_staff" '<<<SELECT StaffID, FirstName, MI,
LastName, Department FROM tblStaff ORDER BY StaffID;
myComm = New SqlDataAdapter(myQuery, cn1)
myComm.SelectCommand.CommandType =
CommandType.StoredProcedure
ds = New DataSet
myComm.Fill(ds, "Staff")
SetupDataGrid(subStaff, ds.Tables("Staff"), , , , True) '
performs binding, adjusts column widths, etc.
Catch ex As Exception
DisplayException(ex, "EJS0504211130")
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSave.Click
Me.BindingContext(ds.Tables("Staff")).EndCurrentEdit()
Dim commandBuilder As SqlClient.SqlCommandBuilder = New
SqlClient.SqlCommandBuilder(myComm)
myComm.Update(ds, "Staff")
Me.Close()
End Sub