By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,479 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,479 IT Pros & Developers. It's quick & easy.

Combo box with IDs for each item added ?

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

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

P: n/a
* "Gerry" <an*******@discussions.microsoft.com> scripsit:
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/groups?selm=eVNKEiThDHA.1696%40TK2MSFTNGP09.phx.gb l>

- or -

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

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
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
"Gerry" <an*******@discussions.microsoft.com> schreef in bericht
news:0a****************************@phx.gbl...
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

Nov 20 '05 #4

P: n/a
Nice one Mattias, thanks.

Gerry
-----Original Message-----
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), justcreate a class with name, ID and any additional information you wantand put it in there. Set the DisplayMember property to Name oroverride ToString to get the right string displayed. When you need theID, 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.comPlease reply only to the newsgroup.
.

Nov 20 '05 #5

P: n/a
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()
///
Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.