PC Review


Reply
Thread Tools Rate Thread

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

 
 
Mike Hofer
Guest
Posts: n/a
 
      25th Sep 2003
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.


 
Reply With Quote
 
 
 
 
Dean Sharp
Guest
Posts: n/a
 
      25th Sep 2003
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" <(E-Mail Removed)> 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to fill a combo box with choice from another combo box Brian Microsoft Excel Programming 7 20th Apr 2010 09:14 AM
combo box selection to auto fill a second combo box (or field) MSA Microsoft Access Forms 1 28th Feb 2008 10:05 PM
Auto Fill In Combo Box From Combo Box Maurita Microsoft Access VBA Modules 4 13th Dec 2006 05:56 AM
Can not bind child table in Combo box or list Box =?Utf-8?B?Uy5TLkNob24=?= Microsoft Dot NET Compact Framework 0 19th Nov 2004 01:07 PM
Fill a combo box from collection, but bind to a custom class? Mike Hofer Microsoft VB .NET 1 25th Sep 2003 04:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:36 PM.