PC Review


Reply
Thread Tools Rate Thread

binding a listbox

 
 
Michael Powe
Guest
Posts: n/a
 
      1st Dec 2007
Hello,

I want to bind a list box to a collection that will be pulled
dynamically from a web service.

The data that will be pulled will consist of a name and id. I want my
listbox to display the name but, when a name is selected, pass the id
as the value.

i've read and googled but i can't seem to find a straightforward way
to do this. in asp.net, apparently, i could do this with the
ListItems array, but that collection does not exist for a Windows
form.

How can I accomplish my objective in the most direct manner?

Thanks.

mp

--
'cat' is not recognized as an internal or external command,
operable program or batch file.
 
Reply With Quote
 
 
 
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      1st Dec 2007
Hi Michael,

Create a List of KeyValuePairs with items from the web service, with ID being the key and Name the value (or opposite). Use this list as a DataSource for the ListBox and set the ListBox.DisplayMember property to "Value" and ListBox.ValueMember to "Value" (or opposite). The name should be shown in the listbox, and SelectedValue will be the ID.

Roughly something like

List<KeyValuePair<int, string>> list = GetListFromWeb();
listBox1.DataSource = list;
listBox1.DisplayMember = "Value";
listBox1.ValueMember = "Key";

On Sat, 01 Dec 2007 05:27:38 +0100, Michael Powe <michael+(E-Mail Removed)> wrote:

> Hello,
>
> I want to bind a list box to a collection that will be pulled
> dynamically from a web service.
>
> The data that will be pulled will consist of a name and id. I want my
> listbox to display the name but, when a name is selected, pass the id
> as the value.
>
> i've read and googled but i can't seem to find a straightforward way
> to do this. in asp.net, apparently, i could do this with the
> ListItems array, but that collection does not exist for a Windows
> form.
>
> How can I accomplish my objective in the most direct manner?
>
> Thanks.
>
> mp
>




--
Happy coding!
Morten Wennevik [C# MVP]
 
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 listbox items mick Microsoft C# .NET 1 11th Mar 2009 09:29 PM
ListBox WPF and Binding SteveT Microsoft C# .NET 2 15th Jan 2008 07:15 AM
listbox binding =?Utf-8?B?TWlrZQ==?= Microsoft VB .NET 1 5th Nov 2005 01:52 PM
Binding dataset to a ListBox. Jason Huang Microsoft C# .NET 2 24th Aug 2005 02:47 PM
Re: ListBox DataTable Binding.. Tony Microsoft Dot NET Framework Forms 0 22nd Jul 2003 08:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:54 AM.