ListBox: How to add an item with its DisplayMember and ValueMember

B

Ben

How would you add an item with its DisplayMember and ValueMember without
binding to a datatable or dataview like:
listbox.DisplayMember = "name";
listbox.ValueMember = "recid";
listbox.DataSource = dt;

listbox.Items.Add(name) can only add the DisplayMember.


Thanks,
Ben
 
P

Peter Duniho

How would you add an item with its DisplayMember and ValueMember without
binding to a datatable or dataview like:
listbox.DisplayMember = "name";
listbox.ValueMember = "recid";
listbox.DataSource = dt;

listbox.Items.Add(name) can only add the DisplayMember.

DisplayMember works with properties too, or any bindable thing really. If
you set the DisplayMemeber to a property of a class, for example, and add
an instance of that class as an item in the ListBox, it should work fine..

So, perhaps you could be more specific about what exactly you want to do
and what trouble you're having doing it.

Pete
 
L

Liz

How would you add an item with its DisplayMember and ValueMember without
binding to a datatable or dataview like:
listbox.DisplayMember = "name";
listbox.ValueMember = "recid";
listbox.DataSource = dt;

listbox.Items.Add(name) can only add the DisplayMember.

DisplayMember and ValueMember only make sense in databound contexts, no?
i.e.: where you're going to assign DataSource

if you're going to populate a listbox with listbox.Items.Add(object) then
you can just pick off whatever you need from the object for display in the
LB and for storage, intermediate processing, etc

class Person
{
private string _name;
private int _id;

Person(string name, int id)
{
_name = name;
_id = id;
}

public override string ToString()
{ return _name; }

public string id
{
get { return _id; }
}

public string name
{
get { return _name; }
}

}

listbox.Items.Add(new Person("Jamie", 100)); // Jamie will be the
"display member" per ToString override
.....
Person p = (Person)listbox.SelectedItem;
int EmployeeID = p.id; // integer
Person ID
string EmpName = p.name; // string Person
name
 
B

Ben

Thanks, this is what I'm looking for.

Liz said:
DisplayMember and ValueMember only make sense in databound contexts, no?
i.e.: where you're going to assign DataSource

if you're going to populate a listbox with listbox.Items.Add(object) then
you can just pick off whatever you need from the object for display in the
LB and for storage, intermediate processing, etc

class Person
{
private string _name;
private int _id;

Person(string name, int id)
{
_name = name;
_id = id;
}

public override string ToString()
{ return _name; }

public string id
{
get { return _id; }
}

public string name
{
get { return _name; }
}

}

listbox.Items.Add(new Person("Jamie", 100)); // Jamie will be the
"display member" per ToString override
.....
Person p = (Person)listbox.SelectedItem;
int EmployeeID = p.id; // integer
Person ID
string EmpName = p.name; // string Person
name
 
S

sloan

#1 Rule about putting items into comboboxes in a winforms application.

You put OBJECTS into the combo boxes, not key/value pairs. (as you do in a
web environment).

as the previous example shows....you're putting a (collection of) Person
objects into the combobox.
 
P

Peter Duniho

Thanks, this is what I'm looking for.

Well...

It's true that ToString() is used by default to display the object. But
I'm not sure that's actually what you were looking for. Overriding
ToString() may or may not be an option, depending on whether it's already
doing something desirable, and it hard-codes the presented value for the
object.

As I wrote, you can set the DisplayMember property for the control to use
any property in the class. IMHO, this is a better way to do it than
overriding ToString() just for that purpose. It's more general-purpose
and can be changed easily and even accomodate different ways to present
the same object.

Pete
 

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