Fill a combo box from collection, but bind to a custom class?

Discussion in 'Microsoft Dot NET Framework Forms' started by Mike Hofer, Sep 25, 2003.

  1. Mike Hofer

    Mike Hofer Guest

    I am trying to do something a little nonstandard, and am having some
    difficulty determining (a) if it can be done and (b) if so, how to do it.

    What I want to do is fill a dropdown combobox with data from a collection.
    Then, I want to bind that list to a member of a custom class. (Don't ask
    why, just trust me on that one. <gr>).

    What I'm doing now is binding the combo box to a collection, as follows:

    Dim items As ArmorTypeCollection = ArmorTypeDac.Select()
    With Me.ArmorType
    .DataSource = items
    .DisplayMember = "Value"
    .ValueMember = "ID"
    End With

    Then, when the data is loaded onto the form, I set the default member as
    follows:

    Dim item As ArmorType
    For Each item In ArmorType.Items
    If item.ID.ToString() = _armor.Type.ID.ToString() Then
    ArmorType.SelectedItem = item
    Exit For
    End If
    Next

    When the user commits her changes, I load them back into the object as
    follows:

    _armor.Type = ArmorTypeDac.Select().Item(Me.ArmorType.SelectedValue())

    Is there a better way to do this? Essentially, my _armor object (of type
    Armor) has a reference to an item in the armor type collection.

    Any help would be greatly appreciated.
     
    Mike Hofer, Sep 25, 2003
    #1
    1. Advertisements

  2. Mike Hofer

    Dean Sharp Guest

    Well the answer to your first question is yes. It can be done. If I
    understand your question, you are wanting to bind a ComboBox to a data
    class. I think the syntax that you are looking for is as follows:

    ComboBox1.Databindings.Clear
    ComboBox1.Databindings.Add ("SelectedItem", objDataClass,
    "myDataClassProperty")

    That's about it. I hope it helps. This is called simple databinding.
    There is also complex databinding.
    --

    Dean


    "Mike Hofer" <> wrote in message
    news:bBDcb.14914$AH4.4423@lakeread06...
    > I am trying to do something a little nonstandard, and am having some
    > difficulty determining (a) if it can be done and (b) if so, how to do it.
    >
    > What I want to do is fill a dropdown combobox with data from a collection.
    > Then, I want to bind that list to a member of a custom class. (Don't ask
    > why, just trust me on that one. <gr>).
    >
    > What I'm doing now is binding the combo box to a collection, as follows:
    >
    > Dim items As ArmorTypeCollection = ArmorTypeDac.Select()
    > With Me.ArmorType
    > .DataSource = items
    > .DisplayMember = "Value"
    > .ValueMember = "ID"
    > End With
    >
    > Then, when the data is loaded onto the form, I set the default member as
    > follows:
    >
    > Dim item As ArmorType
    > For Each item In ArmorType.Items
    > If item.ID.ToString() = _armor.Type.ID.ToString() Then
    > ArmorType.SelectedItem = item
    > Exit For
    > End If
    > Next
    >
    > When the user commits her changes, I load them back into the object as
    > follows:
    >
    > _armor.Type = ArmorTypeDac.Select().Item(Me.ArmorType.SelectedValue())
    >
    > Is there a better way to do this? Essentially, my _armor object (of type
    > Armor) has a reference to an item in the armor type collection.
    >
    > Any help would be greatly appreciated.
    >
    >
     
    Dean Sharp, Sep 25, 2003
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David R

    1st combo box item selected in Form_Load after Data Bind.

    David R, Dec 8, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    2
    Views:
    249
    Herfried K. Wagner [MVP]
    Dec 8, 2003
  2. Erik Moore

    Datagrid: Bind to custom collection, layout at design time

    Erik Moore, Jun 18, 2004, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    271
    Erik Moore
    Jun 18, 2004
  3. usha
    Replies:
    1
    Views:
    306
    Guest
    Jul 14, 2004
  4. Guest

    bind combo box

    Guest, May 2, 2006, in forum: Microsoft Dot NET Framework Forms
    Replies:
    1
    Views:
    191
    Guest
    May 2, 2006
  5. M. G,
    Replies:
    1
    Views:
    427
    FUnky
    May 31, 2006
Loading...

Share This Page