473,396 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Combo box with IDs for each item added ?

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
5 1242
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
* "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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel Hill | last post by:
OK, I have very, VERY basic background knowledge of VB6 and have now upgraded to VB.NET and now I'm struggling to bring up the forms I want. What I am looking to do is to have a click a command...
5
by: Dalan | last post by:
I have been searching the archives in an effort to discover how to derive a dual use of a single combo box - so far no go. I found the piece below which pretty much represents the usage. To...
3
by: mal | last post by:
Sorry for repost - system added to another subject for some reason Have tried numerous ideas from the group to solve this one. It is such a simple example that it should be straightforward ! I...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
3
by: TS | last post by:
Hi all, I posted this question here before, but I'm still having a problem with this code. From my windows form, I opened a connection to a SQL database. I need to generate a combo box in my form...
2
by: Robert | last post by:
Am using a nested continuous bound subform to add multiple records to the underlying table. One of the fields is based on a limit to list combo box. Any suggestions on best way to progressively...
2
by: Dave | last post by:
>From a combo box the user can select a supplier. They then click the order button and are then taken to the orders page. From here comes the problem, I want the user to select a combo box unique...
10
by: themightypea | last post by:
Hi, I'm attempting to create a continuos form which allow the user to edit various fields for each item. Unfortunately, the data I need is spread over multiple tables so binding the control to the...
1
by: Josephbupe | last post by:
Sorry, i didn't know exactly what to name my question. What i need is to create a database for storing office items that will include Desktop computers, Laptops, printers, phones, etc. as...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.