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

M

Mike Hofer

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.
 
D

Dean Sharp

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top