How to bind form control to collection item using string indexer

How to bind form control to collection item using string indexer

Old forum URL: forums.lhotka.net/forums/t/2288.aspx


GlennMiller posted on Tuesday, February 06, 2007

I have a businessbase object that contains a list member of businessbase objects indexed by string. What I would like to do is bind an individual item from the list based on an index to text box controls using the Windows form designer; something like this: Customer.Contacts[“Bob”].Phone.

 

When I create a new DataSource in Visual Studio it recognizes my member collection, but when I create a binding source on my form and attempt to bind to a text box, the collection member is no longer available. I can create another binding source based on the collection, but that always binds to the first row of the collection, not a particular indexed item. I know I can use a grid, but I don't want to use that interface for this project.

 

public class Customer : BusinessBase<Customer>

{

private ContactList list;                  // derived from ReadOnlyListBase

 

public ContactList Contacts

{

    get { return list; }

}

}

 

public class ContactList : ReadOnlyListBase<ContactList, Contact>

{

public Contact this[string name]

{

    get { … search for name and return instance }

}

}

 

public class Contact : BusinessBase<Contact>

{

            private string phone;

            

            public string Phone

{

    get { return phone; }

}

}

 

Any ideas how to use the form designer to bind a control to a specific string index of a collection?

 

Thanks.

brian_peterson replied on Thursday, February 08, 2007

Yeah I am interested in this as well. Anyone else have any thoughts on this?

xal replied on Thursday, February 08, 2007

Ok, so let me see if I get this straight...
You have a collection of items and a grid with a corresponding binding source that displays the list.
You want to display a set of controls so that the current selected item in the grid is bound to that set of controls.

Is this the scenario?

If it is, is your set of controls in it's own user control?

Here's the deal:
If you create your own user control to host the "editing" version and then you add the usercontrol to the form with the grid, it won't work straight off. The reason is that the user control uses it's own binding context and thus it's own currency manager. If this is the case, all you need to do is handle the "CurrentItemChanged" event in the bindingsource that's bound to the grid, and pass the value of the binding source's "Current" property to your user control. That will keep both things in sync. (Be aware that you may need to do the EndEdit in your UC's binding source before setting the newly focused item)

If you don't have a user control and the form has everything contained by itself, then it's ridiculously simple:

From the Datasources window, select the collection node you want to use, drop down the little combobox and choose a datagridview. Drag that node to the form and it'll create the grid and a bindingsource. Now go back to that same node and change it to "Details". Once again, drag it to the form and it'll create the controls and use the same binding source. Now both will be synced automatically and all you need to do is set the bindingsource.DataSource property to your actual collection when the form loads.



I hope this brings some light to the matter.

Andrés

cash_pat replied on Thursday, February 08, 2007

If I am not wrong did u mean this??


DataBindingSource.DataSource = mMasterTable
DataBindingSource.DataMember = "Children"

TextEdit1.DataBindings.Add(New System.Windows.Forms.Binding("EditValue", _
    DataBindingSource.Item(0), "EmployeeName", True, _
        System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))

xal replied on Thursday, February 08, 2007

No, you can't use a datamember because it's only one collection with children. You'd use a datamember if those children had child collections themselves and you wanted to display information about those grandchildren in a grid or in a binding navigator...
Also, in my example I'm assuming that the databindings are created through the designer instead of by hand.

Andrés

Copyright (c) Marimer LLC