Combobox - How to match item index with record ID

  • Thread starter Thread starter Bill nguyen
  • Start date Start date
B

Bill nguyen

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
 
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)
 
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
 
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
 
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...
 
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
 
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
 
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
 
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
 
Bill said:
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...
 

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

Back
Top