ListBox, DataReader, ID and Description

  • Thread starter Thread starter Guy Dillen
  • Start date Start date
G

Guy Dillen

I would like using a DataReader to fille a listbox with descriptions, on the
other hand i need to store somwhere an ID (to the respective Descriptions),
that i can use as a key when a listbox item has ben selected.

I know i can use a second invisible listbox that stores the ID's in it, but
are there alternatives to solve this problem (with a DataReader) more
elegant? Maybe using the Tag property?

Thanks
Guy
 
Guy Dillen said:
I would like using a DataReader to fille a listbox with descriptions, on
the other hand i need to store somwhere an ID (to the respective
Descriptions), that i can use as a key when a listbox item has ben
selected.

Take a look at the control's 'DisplayMember', 'ValueMember', and
'DataSource' properties.
 
Ok, i already did, but does DataSource/DisplayMember/ValueMember also work
with a DataReader?

Guy
 
I don't think it's possible, but why use DataReader ???
I implemented it filling a table, added to a DataSet through a DataAdapter:

Dim DS as DataSet
Dim N as string="TableName"
Dim SQL as string="SELECT * FROM ..."
Dim CN as New SQLConnection(ConnectionString)
Dim CMD As New SqlCommand(SQL, CN), DA As New SqlDataAdapter(CMD)
DS.Tables.Add(N) : DA.Fill(DS.Tables(N))
ListBox.ValueMember = ID
ListBox.DisplayMember = Description
ListBox.DataSource = DS.Tables(N)
ListBox.SelectedIndex = -1 : ListBox.SelectedIndex = -1

I hope this can help You
nq





Guy Dillen said:
Ok, i already did, but does DataSource/DisplayMember/ValueMember also work
with a DataReader?

Guy
 
Back
Top