469,299 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Combobox - How to match item index with record ID

Table A has 2 columns:
recID int
recName String

I need to load all recName into combobox B using B.Items.Add(recName). This
is no problem.
However, I would like to be able to match ComboxBox B's index with table A's
recID so that I can use B.SelectedIndex to update any table that uses Table
A's recID. Currently, I have to use a clumsy routine to find recID from
B.text
Any help is greatly appreciated.
Thanks

Bill
Nov 21 '05 #1
11 2449
Change from using Items.Add to a method that uses DataSource and
DisplayMember -- this will allow you to bind a more complex data object
to the ComboBox while maintaining other (non-visible) information -- a
DataView will allow you to filter the orginal data and is bindable to a
combobox (any class that implements IList is bindable)

Nov 21 '05 #2
May I have some sample codes to review?
Thanks
Bill

"stand__sure" <st*********@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Change from using Items.Add to a method that uses DataSource and
DisplayMember -- this will allow you to bind a more complex data object
to the ComboBox while maintaining other (non-visible) information -- a
DataView will allow you to filter the orginal data and is bindable to a
combobox (any class that implements IList is bindable)

Nov 21 '05 #3
Bill,
\\\
Private Loaded as Boolean
////
\\\\
B.Datasource = A
B.DisplayMember = "recName"
B.ValueMember = "recId"
Loaded = true
///
At SelectedIndexChange
\\\
if Loaded then
Dim theId = B.SelectedValue
End if
////

I hope this helps,

Cor
Nov 21 '05 #5
Cor;

This helps, definitely!

However, the combobox somehow display the Valuemember instead of
DisplayMember!
What did I do wrong?

Thanks
Bill
-----------
Here's my codes:
Private Function AddTestItem()

Dim dMySet As DataSet

Dim dmySQL As String

dmySQL = "SELECT * from MP_PreferredRoadTemplate"

dMySet = MAPPOINTDataBoss.dWFIRecView(dmySQL)

' MsgBox(dMySet.Tables(0).Rows.Count)

Dim cbTest As New ComboBox

cbTest.DataSource = dMySet.Tables(0)

cbTest.ValueMember = "roadTemplateID"

cbTest.DisplayMember = "roadTemplatename"
End Function

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Bill,
\\\
Private Loaded as Boolean
////
\\\\
B.Datasource = A
B.DisplayMember = "recName"
B.ValueMember = "recId"
Loaded = true
///
At SelectedIndexChange
\\\
if Loaded then
Dim theId = B.SelectedValue
End if
////

I hope this helps,

Cor

Nov 21 '05 #6
methinks that this isn't the combobos that you are seeing... I don't
see you adding it to the form...

create the combobox on the form and then set its properties in the
method...

Nov 21 '05 #7
Bill,

However, the combobox somehow display the Valuemember instead of
DisplayMember!
What did I do wrong?


Can you show that piece of code frome you,

Cor
Nov 21 '05 #8
Cor & Stand__Sure;
I've tried several things and it's still NOT working properly.
I have a combobox (cbServiceCode) created on the form, so there's no need to
initialize the combobox

below is the code. I hope that you can help me to solve the problem!
Thanks in advance

Bill
1. Use a dataset as cbServiceCode.DataSource. Got error Cannot add new
valuemember!

Private Function AddServiceCode()

Dim dS As DataSet

Dim dSQL As String

Dim mServiceCode, mServiceDesc, mServiceID As String

' Dim mServiceID As Integer

dSQL = "Select * from AC_oxyServices order by oxyServiceCode "

dS = AC_DataBoss.dWFSelectView(dSQL)

cbServiceCode.DataSource = dS

cbServiceCode.DisplayMember = "oxyservicecode"

cbServiceCode.ValueMember = "oxyserviceid"

End Function

2. Use an ArrayList as datasource along with a class (AC_DisplayList)
showing longname and shortname

I got err msg "Argument Prompt cannot be converted to type String" everytime
I click on the box

The box shows the Valuemember instead of displaymember!

Private Function AddServiceCode()

Dim dS As DataSet

Dim dSQL As String

Dim mServiceCode, mServiceDesc, mServiceID As String

' Dim mServiceID As Integer

dSQL = "Select * from AC_oxyServices order by oxyServiceCode "

dS = AC_DataBoss.dWFSelectView(dSQL)

' MsgBox(dS.Tables(0).Rows.Count)

Dim dSRow As DataRow


' Populates the list box using DataSource.

' DisplayMember is used to display just the long name of each state.

Dim mServiceArray As New ArrayList

For Each dSRow In dS.Tables(0).Rows

mServiceCode = Trim(dSRow.Item("oxyServiceCode"))

mServiceDesc = Trim(dSRow.Item("oxyServiceDesc"))

mServiceID = CStr(dSRow.Item("oxyServiceID"))

mServiceArray.Add(New AC_DataBoss.AC_DisplayList(mServiceCode, mServiceID))

'cbServiceCode.Items.Add(m)

Next

cbServiceCode.DataSource = mServiceArray

cbServiceCode.DisplayMember = "LongName"

cbServiceCode.ValueMember = "ShortName"

End Function

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uZ*************@TK2MSFTNGP12.phx.gbl...
Bill,

However, the combobox somehow display the Valuemember instead of
DisplayMember!
What did I do wrong?


Can you show that piece of code frome you,

Cor

Nov 21 '05 #9
Bill,

I think that it is dead simple. You told us that the datasource was tabler
A.
Now I see that your datasource is a complete dataset.

That normally gives only an empty combobox.

cbServiceCode.DataSource = dS.Tables(0)

I stopped with reading when I saw this by the way.

I hope it helps,

Cor
Nov 21 '05 #10
Cor;

Thanks for pointing it out.

I got displaying problem after that, but it was the order and
case-sensitive issue with the column names.

Thanks for your help!

Bill

"Cor Ligthert" <no************@planet.nl> wrote in message
news:u5**************@TK2MSFTNGP10.phx.gbl...
Bill,

I think that it is dead simple. You told us that the datasource was tabler
A.
Now I see that your datasource is a complete dataset.

That normally gives only an empty combobox.

cbServiceCode.DataSource = dS.Tables(0)

I stopped with reading when I saw this by the way.

I hope it helps,

Cor

Nov 21 '05 #11
Bill Nguyen wrote:
I got displaying problem after that, but it was the order and
case-sensitive issue with the column names.


sorry, I was away for a bit... glad to see that you got it sorted out...

Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by George Addison | last post: by
2 posts views Thread by Adam Knight | last post: by
2 posts views Thread by Thomas E. Nørgaard | last post: by
reply views Thread by Bruno Neves Pires Silva | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.