473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Dec 20 '07 #1
5 33986
On Wed, 19 Dec 2007 19:25:00 -0800, Ben <Be*@discussions.microsoft.com
wrote:
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
Dec 20 '07 #2
Liz

"Ben" <Be*@discussions.microsoft.comwrote in message
news:5A**********************************@microsof t.com...
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


Dec 20 '07 #3
Thanks, this is what I'm looking for.

"Liz" wrote:
>
"Ben" <Be*@discussions.microsoft.comwrote in message
news:5A**********************************@microsof t.com...
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


Dec 20 '07 #4

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


"Ben" <Be*@discussions.microsoft.comwrote in message
news:09**********************************@microsof t.com...
Thanks, this is what I'm looking for.

"Liz" wrote:
>>
"Ben" <Be*@discussions.microsoft.comwrote in message
news:5A**********************************@microso ft.com...
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



Dec 20 '07 #5
On Thu, 20 Dec 2007 03:08:00 -0800, Ben <Be*@discussions.microsoft.com>
wrote:
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
Dec 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Supa Hoopsa | last post by:
Working on a windows form, I am trying to bind a listbox to a dataset and want to set the displaymember & Valuemember properties of the listbox so that I can retrieve the selectedvalue from a...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
1
by: Irwin M. Fletcher | last post by:
ListBox DataSource DisplayMember Property Problems in C# I have a (single select) listbox with data and when I click on the list I can't get the right text selected. My listbox is setup...
0
by: E Miller | last post by:
Visual Studio .NET 2002 I've got an application in which I need to be able to click on a listbox item to initiate a dialog pertaining to that item. I'd like to be able to keep the dialog...
1
by: Claire | last post by:
As a Listbox.item, rather than using a string I'm using a ListItems object, overriding the ToString method When I add items to listbox.items, my strings are shown correctly. On the otherhand, when...
0
by: Robert Karlsson | last post by:
Hi, Is there a way of generating a tab within a ListBox item to create the illusion of columns within the item? Example: Header 1 Header 2 <- labels...
21
by: Bilal Abbasi | last post by:
I realize that you can add items to a list box as objects so you can have access to more than just one property like the itemindex in vb6. Question I have is how do I cause the listbox to show a...
3
by: Dean Slindee | last post by:
In a checked listbox, I am allowing drag/drop of the items within (resequencing). Problem is, when dropping a checked item, the checked state always reverts to unchecked (unwanted). Anyone know...
2
by: baret bonden | last post by:
Trying to return a selected listbox item to another form .tried lots of ways; defining public variables and passing those as well as textboxes ..I' m able to display the chosen item on it's form...
8
by: Dan | last post by:
Is there any way to assign a tag value to each item in a listbox?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.