vb.net 2005 - how to populate a listbox

B

bill

Please direct me to a source which will explain how to populate a listbox
with retrievable values.

For example, I would like to display the customer name from the customer
table, but store the Customer ID which is available when an customer is
selected from the listbox.

Thanks
 
S

Steve Lloyd

Hi bill,

If you create a DataTable from your customer table with ID and Name in it
you can set it as the DataSource of your list box, then set DisplayMember
and ValueMember to the name and ID respectively.

You then call the ListBox.SelectedValue in the postback.

Steve.
 
C

Cor Ligthert [MVP]

Bill,

Dim dtIDA As New DataTable
dtIDA.Columns.Add("IDA", GetType(System.String))
dtIDA.Columns.Add("Description")
dtIDA.LoadDataRow(New Object() {"FQ", "Faqs"}, True)
dtIDA.LoadDataRow(New Object() {"DX", "DirextX"}, True)
dtIDA.LoadDataRow(New Object() {"LK", "Links"}, True)
dtIDA.DefaultView.Sort = "Description"
lstTitles.DataSource = dtIDA.DefaultView
lstTitles.DisplayMember = "Description"
lstTitles.ValueMember = "IDA"

Do you mean something as above

I hope this helps,

Cor
 
B

bill

Thanks!

Steve Lloyd said:
Hi bill,

If you create a DataTable from your customer table with ID and Name in it
you can set it as the DataSource of your list box, then set DisplayMember
and ValueMember to the name and ID respectively.

You then call the ListBox.SelectedValue in the postback.

Steve.
 
B

bill

Thanks Cor.
Cor Ligthert said:
Bill,

Dim dtIDA As New DataTable
dtIDA.Columns.Add("IDA", GetType(System.String))
dtIDA.Columns.Add("Description")
dtIDA.LoadDataRow(New Object() {"FQ", "Faqs"}, True)
dtIDA.LoadDataRow(New Object() {"DX", "DirextX"}, True)
dtIDA.LoadDataRow(New Object() {"LK", "Links"}, True)
dtIDA.DefaultView.Sort = "Description"
lstTitles.DataSource = dtIDA.DefaultView
lstTitles.DisplayMember = "Description"
lstTitles.ValueMember = "IDA"

Do you mean something as above

I hope this helps,

Cor
 
T

Tim Anderson

bill said:
Please direct me to a source which will explain how to populate a listbox
with retrievable values.

The nice thing about the listbox is that the items property is just a
collection of objects. An ArrayList will do. When the listbox is rendered,
it the Framework calls ToString() on each object. Each object could be your
custom class with ID and CustomerName properties, or whatever you like. So
while databinding also works fine, it is really very flexible.

Tim
How stable is Visual Studio 2005?
http://www.itwriting.com/blog/?postid=414
 

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