Storing objects in Combo and List box

J

jay

I have a structure:


public struct DATA_PAIR
{
public int ID;
public string Value;
public DATA_PAIR(int id, string val)
{
this.ID = id;
this.Value = val;
}
}

I have a combo box and I'm adding items:

....
ArrayList locations = new ArrayList();
locations.Add(new ORCASettings.Common.DATA_PAIR(0, ""));
while (Reader.Read())
{
locations.Add(new ORCASettings.Common.DATA_PAIR((int)Reader[0],
Reader[9].ToString()));
}
cboLocation.ValueMember = "ID" ;
cboLocation.DisplayMember = "Value";
cboLocation.DataSource = locations;
....


....but this doesn't work. ValueMember and DisplayMember don't get affected.
What I get in my combo box is Type of the objects: ClassName+DATA_PAIR.

Thanks,
h
 
J

Joe Mayo [C# MVP]

Hi Jay,

You need to define ID and Value as public properties of DATA_PAIR, rather
than public fields - makes for better encapsulation too.

Joe
 
J

jay

Thanks, it works now!

Joe Mayo said:
Hi Jay,

You need to define ID and Value as public properties of DATA_PAIR, rather
than public fields - makes for better encapsulation too.

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com


I have a structure:


public struct DATA_PAIR
{
public int ID;
public string Value;
public DATA_PAIR(int id, string val)
{
this.ID = id;
this.Value = val;
}
}

I have a combo box and I'm adding items:

...
ArrayList locations = new ArrayList();
locations.Add(new ORCASettings.Common.DATA_PAIR(0, ""));
while (Reader.Read())
{
locations.Add(new ORCASettings.Common.DATA_PAIR((int)Reader[0],
Reader[9].ToString()));
}
cboLocation.ValueMember = "ID" ;
cboLocation.DisplayMember = "Value";
cboLocation.DataSource = locations;
...


...but this doesn't work. ValueMember and DisplayMember don't get affected.
What I get in my combo box is Type of the objects: ClassName+DATA_PAIR.

Thanks,
h
 

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