Combo box with IDs for each item added ?

  • Thread starter Thread starter Gerry
  • Start date Start date
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
 
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
 
* "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).
 
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
 
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.
.
 
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()
///
 
Back
Top