Updating a Database from DataBound Controls

Posted by Avatar_Squadron on Stack Overflow See other posts from Stack Overflow or by Avatar_Squadron
Published on 2008-11-03T21:19:09Z Indexed on 2010/04/05 15:03 UTC
Read the original article Hit count: 190

Filed under:
|
|

Hi. I'm currently creating a WinForm in VB.NET bound to an access database.

Basically what i have are two forms: one is a search form used to search the database, and the other is a details form. You run a search on the searchForm and it returns a list of Primary Keys and a few other identifying values. You then double click on the entry you want to view, and it loads the details form.

The Details form has a collection of databound controls to display the data: mostly text boxes and checkboxs. The way i've set it up is i used the UI to build the form and then set the DataBindings Property of each control to "TblPropertiesBindingSource - " where value name is one of the values in the table (such as PropertyID or HasWoodFloor).

Then, when you double click an entry in the searchform, I handle the event by parsing the Primary Key (PropertyID) out of the selected row and then storing this to the details form:

Note: Detail is the details form that is opened to display the info

Private Sub propView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles propView.CellDoubleClick
        Dim detail As frmPropertiesDetail = New frmPropertiesDetail
        detail.id = propView.Rows(e.RowIndex).Cells(0).Value
        detail.Show()
    End Sub

Then, upon loading the details form, it set's the filter on the BindSource as such:

TblPropertiesBindingSource.Filter() = "PropertyID=" & id

This works great so far. All the controls on the details form will display the correct info. The problem is updating changes.

Scenario: If i have the user load the details for say, property 10001, it will show a description in a textBox named descriptionBox which is identical to the value of the description value of for that entry in the database. I want the user to then be able to change the text of the text box (which they can currently do) and click the save button (saveBut) and have the form update all the values in the controls to the database.

Theorectically, it should do this as the controls are DataBound, thus i can avoid writing code that tells each entry in the database row to take the value of the aligned control.

I've tried calleding PropertiesTableAdapter.Update(PropertiesBindingSource.DataSource), but that doesnt seem to do it.

© Stack Overflow or respective owner

Related posts about vb

Related posts about databinding