Combo box with IDs for each item added ?

G

Gerry

I need to populate a drop-down list box with names from a
table (via SQL Stored Proc). Each name has a unique ID
associated with them e.g.

NameID 1
Name "My Name"

The problem is how can I tell the combo box the ID
associated with each name added to it.

(I used to use Itemdata from vb6) - is there
something similar/or better I could use here ?

Thanks
Gerry
 
M

Mattias Sjögren

Gerry,
(I used to use Itemdata from vb6) - is there
something similar/or better I could use here ?

Since the combobox can store any objects (not just strings), just
create a class with name, ID and any additional information you want
and put it in there. Set the DisplayMember property to Name or
override ToString to get the right string displayed. When you need the
ID, get the selected item and cast to your item class.



Mattias
 
H

Herfried K. Wagner [MVP]

* "Gerry said:
I need to populate a drop-down list box with names from a
table (via SQL Stored Proc). Each name has a unique ID
associated with them e.g.

NameID 1
Name "My Name"

The problem is how can I tell the combo box the ID
associated with each name added to it.

(I used to use Itemdata from vb6) - is there
something similar/or better I could use here ?

<http://groups.google.de/[email protected]>

- or -

Databound listbox (have a look at its 'DataSource' property and the
'DisplayMember' and 'ValueMember' properties).
 
C

Cor

Hi Gerry,
There is a Combobox for a windowforms and a Dropdownlist for a webform,
That makes the answer not easier.
If you both populate them using a dataset it is very easy.
for the combobox you can use (have a look for) the
datasource
displaymember
valuemember
for the dropdownlist you can use (have a look for) the
datasource
datatexfield
datavaluefield

If you do not use a dataset you have to make objects for that , that is not
difficult, but needs another approach.

I hope this helps?

Cor

Cor
 
G

Gerry

Nice one Mattias, thanks.

Gerry
-----Original Message-----
Gerry,


Since the combobox can store any objects (not just strings), just
create a class with name, ID and any additional information you want
and put it in there. Set the DisplayMember property to Name or
override ToString to get the right string displayed. When you need the
ID, get the selected item and cast to your item class.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
.
 
C

Cor

Hi Gerry,

I forgot that while I was answering you almost this was before my nose

But I had the idea it was a dropdownlist

Maybe you can use it

Cor

\\\
Dim dt As New DataTable
dt.Columns.Add("A")
dt.Columns.Add("B")
For i As Integer = 0 To 50
dt.Rows.Add(dt.NewRow)
dt.Rows(i)(0) = Chr(i + 66)
dt.Rows(i)(1) = i.ToString
Next
Me.ComboBox1.BeginUpdate()
dv = New DataView(dt)
Me.ComboBox1.DataSource = dv
dv.Sort = "A ASC"
Me.ComboBox1.DisplayMember = "A"
Me.ComboBox1.ValueMember = "B"
Me.ComboBox1.DataSource = dv
Me.ComboBox1.DisplayMember = "A"
Me.ComboBox1.ValueMember = "B"
Me.ComboBox1.EndUpdate()
///
 

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