views:

274

answers:

2

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.

A: 

The site is down, so use the google cached version of the following link:

http://www.paulstovell.com/blog/binding-oriented-programming

It's near exact to what you're trying to do.

Gavin Miller
This was more of a "Why you should use it" and not a How to, but it was still insightful and useful. I'd vote you up, but i lack the rep :-\
Avatar_Squadron
A: 

Ok, I was able to figure this out picking apart some code I pillaged from a friend.

Everything was ok, the problem was the updating

When I was saving the data, i was calling just:

Me.TblProptertiesTableAdapter.Update(Me.TblPropertiesBindingSource.DataSource)

The correct code, without changing anything else is:

Me.Validate()
Me.TblPropertiesBindingSource.EndEdit()
Me.TblPropertiesTableAdapter.Update(Me.RentalPropertiesDataSet.tblProperties)
Me.RentalPropertiesDataSet.AcceptChanges()

Where RentalPropertiesDataSet is the database where TblProperties comes from. Inorder for this to work, make sure TblPropertiesBindingSource.DataSource is RentalPropertiesDataSet.Properties This was autosetup for me by VS08 when it created the BindingSource.

Basically, I needed to tell teh BindingSource to stop allowing the fields to be edited. Then we save the changes to the database, and lastly we tell the DataBase to accept the changes.

Avatar_Squadron