Groups | Blog | Home
all groups > dotnet windows forms databinding > february 2006 >

dotnet windows forms databinding : From Datagrid to DataBase


Sylvain Bissonnette
2/21/2006 8:25:08 PM
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
2/22/2006 7:44:29 PM
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.

[quoted text, click to view]

JSantora
2/23/2006 8:46:30 AM
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
AddThis Social Bookmark Button