databinding to multiple controls

Posts   
 
    
BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 27-Sep-2010 22:35:37   

version 3.0, selfserving, sql server

I have been trying to figure out how to use databinding on one of my windows forms in my project and I got it to work but I hope there is an easier way to do what I am trying to do.

I have a datgridview that is bound to a collection called Stores that is filled with the following:

    StoreCollection.GetMulti(nothing)

It only a few fields, such as StoreID and name. If a user selects an existing store and selects an edit button, I display another form that uses a binding source that is bound to a stores collection that I create using the following:

    Dim filter As New PredicateExpression(StoreFields.LocId = StoreID)
    StoreCollection.GetMulti(filter)

On my edit form there are multiple (about 30) different controls that are bound to the StoreCollection. All works fine with an existing record. But, if the user selects a store that does not exist I do the following:

        PBStoreLocationBindingSource.AddNew()

In code I set some of the controls to a default value as follows:

       TextBox1.Text = 300
       ComboBox5.SelectedIndex = 1

The first control on the screen is a textbox that is bound to the field storename. The first problem I had was that everytime I entered a store name in the textbox and went to the next control, ALL of the controls that I had programically filled with the above code would reset. I took care of this by going to the properties of each control under databindings, advanced and changing the Data Source Update Mode to OnPropertyChanged.

With this it looked like it was fine until I actually tried to update a new record. When I tried to update the new record I would get an erro saying that one of the fields, IP1 does not all nulls. The text on the textbox that is bound to that field is infact "0". The way I got around this is by doing the following:

        TextBox11.Text = 1
        TextBox11.Text = 0

Then the record wrote fine.

Is guess my question is, Do I hve to jump through all of these hoops with the controls and the defaults? Or is there a easier way to accomplish this task?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Sep-2010 09:11:38   

If a user selects an existing store and selects an edit button, I display another form that uses a binding source that is bound to a stores collection that I create using the following:

    Dim filter As New PredicateExpression(StoreFields.LocId = StoreID)
    StoreCollection.GetMulti(filter)

Please explain why do you bind to a collection in the Edit form? Aren't you just editing one item at a time?

BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 28-Sep-2010 14:10:21   

I guess I am binding to a collection on the edit form because that is the way I was told to do it. Should I be doing it differently? And if so, how and will it make binding easier?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 28-Sep-2010 21:30:23   

You can bind to a single entity as easily as to a collection of enties. This is what I always do on single item edit forms. You also do not need to worry about the binding manager - in code just set the controls' databinding property to your entity properties.

txtName.Databindings.Add("Text",yourEntity,"Name");

I thinks this creates a binding context/binding manager in the background, but I can't remember the last time I needed to access one directly.

Matt

BISHMAN
User
Posts: 41
Joined: 30-Jan-2010
# Posted on: 28-Sep-2010 22:58:00   

Thank you very much for your help. This helped alot.