vb.net 2005 - how to populate a listbox

  • Thread starter Thread starter bill
  • Start date Start date
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
 
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.
 
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
 
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.
 
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
 
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
 
Back
Top