PC Review


Reply
Thread Tools Rate Thread

Combobox Datasource & Binding Custom Objects

 
 
Demetri
Guest
Posts: n/a
 
      22nd Sep 2009
I have a structure called SelectionItem and it has two properties - Key&
Value. I also have a combobox with the DisplayMember = Value and ValueMember
= Key. I have the following simple code:

List<SelectionItem> list = new List<SelectionItem>();
list.Add(new SelectionItem(0, "Item1"));
list.Add(new SelectionItem(1, "Item2"));
myCboBox.DataSource = list;

My issue is that during runtime the cbobox doesn't have Item1, Item2 as
possible values. Rather it has the type name of the object (i.e.
MyApp.UI.Forms.SelectionItem) for each item.

What am I doing wrong?
 
Reply With Quote
 
 
 
 
miher
Guest
Posts: n/a
 
      23rd Sep 2009
Hi,

Are You sure that the DisplayMember and the ValueMember properties are set ?

-Zsolt

"Demetri" <(E-Mail Removed)> wrote in message
news:F68074A9-26D5-48A0-A3EC-(E-Mail Removed)...
> I have a structure called SelectionItem and it has two properties - Key&
> Value. I also have a combobox with the DisplayMember = Value and
> ValueMember
> = Key. I have the following simple code:
>
> List<SelectionItem> list = new List<SelectionItem>();
> list.Add(new SelectionItem(0, "Item1"));
> list.Add(new SelectionItem(1, "Item2"));
> myCboBox.DataSource = list;
>
> My issue is that during runtime the cbobox doesn't have Item1, Item2 as
> possible values. Rather it has the type name of the object (i.e.
> MyApp.UI.Forms.SelectionItem) for each item.
>
> What am I doing wrong?


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      23rd Sep 2009
"Demetri" <(E-Mail Removed)> wrote in message
news:F68074A9-26D5-48A0-A3EC-(E-Mail Removed)...

>I have a structure called SelectionItem and it has two properties - Key&
> Value. I also have a combobox with the DisplayMember = Value and
> ValueMember
> = Key. I have the following simple code:
>
> List<SelectionItem> list = new List<SelectionItem>();
> list.Add(new SelectionItem(0, "Item1"));
> list.Add(new SelectionItem(1, "Item2"));
> myCboBox.DataSource = list;
>
> My issue is that during runtime the cbobox doesn't have Item1, Item2 as
> possible values. Rather it has the type name of the object (i.e.
> MyApp.UI.Forms.SelectionItem) for each item.
>
> What am I doing wrong?


Show us the actual definition of the SelectionItem structure, plus the code
that is setting the DisplayMember and ValueMember proeprties on the combo
box.

Personally, I just always override ToString() and provide my display text
that way.


 
Reply With Quote
 
Jack Jackson
Guest
Posts: n/a
 
      23rd Sep 2009
On Tue, 22 Sep 2009 12:32:22 -0700, Demetri
<(E-Mail Removed)> wrote:

>I have a structure called SelectionItem and it has two properties - Key&
>Value. I also have a combobox with the DisplayMember = Value and ValueMember
>= Key. I have the following simple code:
>
>List<SelectionItem> list = new List<SelectionItem>();
>list.Add(new SelectionItem(0, "Item1"));
>list.Add(new SelectionItem(1, "Item2"));
>myCboBox.DataSource = list;
>
>My issue is that during runtime the cbobox doesn't have Item1, Item2 as
>possible values. Rather it has the type name of the object (i.e.
>MyApp.UI.Forms.SelectionItem) for each item.
>
>What am I doing wrong?


Is SelectionItem a Structure and not a Class? I don't think this
works for Structures, only Classes.
 
Reply With Quote
 
Demetri
Guest
Posts: n/a
 
      24th Sep 2009
Sorry, my bad; It is a class:

public class SelectionItem
{
public SelectionItem(int key,string value)
{
Key = key;
Value = value;
}

public int Key
{
get;set;
}

public string Value
{
get;set;
}
}

"Jack Jackson" wrote:

> On Tue, 22 Sep 2009 12:32:22 -0700, Demetri
> <(E-Mail Removed)> wrote:
>
> >I have a structure called SelectionItem and it has two properties - Key&
> >Value. I also have a combobox with the DisplayMember = Value and ValueMember
> >= Key. I have the following simple code:
> >
> >List<SelectionItem> list = new List<SelectionItem>();
> >list.Add(new SelectionItem(0, "Item1"));
> >list.Add(new SelectionItem(1, "Item2"));
> >myCboBox.DataSource = list;
> >
> >My issue is that during runtime the cbobox doesn't have Item1, Item2 as
> >possible values. Rather it has the type name of the object (i.e.
> >MyApp.UI.Forms.SelectionItem) for each item.
> >
> >What am I doing wrong?

>
> Is SelectionItem a Structure and not a Class? I don't think this
> works for Structures, only Classes.
>

 
Reply With Quote
 
coloncm
Guest
Posts: n/a
 
      2nd Oct 2009
I hope you got this by now, but you seem to have re-invented the wheel. Why
don't you just the KeyValuePair object, instead. Even if you're doing
something fancy with your SelectionItem, you can pass it to the list of
KeyValuePair objects and easily load the combo box.

Otherwise, it could have something to with setting DisplayMember and
ValueMember properties before setting the DataSource property on the combo
box, which might reset those properties.

Good luck,

"Demetri" <(E-Mail Removed)> wrote in message
news:F68074A9-26D5-48A0-A3EC-(E-Mail Removed)...
> I have a structure called SelectionItem and it has two properties - Key&
> Value. I also have a combobox with the DisplayMember = Value and
> ValueMember
> = Key. I have the following simple code:
>
> List<SelectionItem> list = new List<SelectionItem>();
> list.Add(new SelectionItem(0, "Item1"));
> list.Add(new SelectionItem(1, "Item2"));
> myCboBox.DataSource = list;
>
> My issue is that during runtime the cbobox doesn't have Item1, Item2 as
> possible values. Rather it has the type name of the object (i.e.
> MyApp.UI.Forms.SelectionItem) for each item.
>
> What am I doing wrong?


 
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
Binding custom objects to dropdown 3P Microsoft ASP .NET 0 21st Feb 2010 02:33 PM
Using ComboBox in DataGridView with custom classes as datasource Jan Obrestad Microsoft Dot NET Framework Forms 0 28th Sep 2007 08:03 AM
Binding custom objects Alla Microsoft Dot NET Framework Forms 2 14th Mar 2007 02:06 PM
Binding Custom Collection/Objects Merk Microsoft C# .NET 2 9th Sep 2006 10:28 PM
Binding custom objects and comboboxes Stevo Microsoft Dot NET Framework Forms 0 11th Aug 2005 08:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:42 AM.