Combobox - How to match item index with record ID

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill nguyen

    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(rec Name). 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


  • stand__sure

    #2
    Re: Combobox - How to match item index with record ID

    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)

    Comment

    • Bill nguyen

      #3
      Re: Combobox - How to match item index with record ID

      May I have some sample codes to review?
      Thanks
      Bill

      "stand__sur e" <stand__sure@ho tmail.com> wrote in message
      news:1116733892 .553398.23980@g 47g2000cwa.goog legroups.com...[color=blue]
      > 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)
      >[/color]


      Comment

      • stand__sure

        #4
        Re: Combobox - How to match item index with record ID



        Comment

        • Cor Ligthert

          #5
          Re: Combobox - How to match item index with record ID

          Bill,
          \\\
          Private Loaded as Boolean
          ////
          \\\\
          B.Datasource = A
          B.DisplayMember = "recName"
          B.ValueMember = "recId"
          Loaded = true
          ///
          At SelectedIndexCh ange
          \\\
          if Loaded then
          Dim theId = B.SelectedValue
          End if
          ////

          I hope this helps,

          Cor


          Comment

          • Bill nguyen

            #6
            Re: Combobox - How to match item index with record ID

            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_PreferredRoa dTemplate"

            dMySet = MAPPOINTDataBos s.dWFIRecView(d mySQL)

            ' MsgBox(dMySet.T ables(0).Rows.C ount)

            Dim cbTest As New ComboBox

            cbTest.DataSour ce = dMySet.Tables(0 )

            cbTest.ValueMem ber = "roadTemplateID "

            cbTest.DisplayM ember = "roadTemplatena me"


            End Function

            "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
            news:%238FSzlpX FHA.2768@tk2msf tngp13.phx.gbl. ..[color=blue]
            > Bill,
            > \\\
            > Private Loaded as Boolean
            > ////
            > \\\\
            > B.Datasource = A
            > B.DisplayMember = "recName"
            > B.ValueMember = "recId"
            > Loaded = true
            > ///
            > At SelectedIndexCh ange
            > \\\
            > if Loaded then
            > Dim theId = B.SelectedValue
            > End if
            > ////
            >
            > I hope this helps,
            >
            > Cor
            >[/color]


            Comment

            • stand__sure

              #7
              Re: Combobox - How to match item index with record ID

              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...

              Comment

              • Cor Ligthert

                #8
                Re: Combobox - How to match item index with record ID

                Bill,
                [color=blue]
                >
                > However, the combobox somehow display the Valuemember instead of
                > DisplayMember!
                > What did I do wrong?
                >[/color]

                Can you show that piece of code frome you,

                Cor


                Comment

                • Bill nguyen

                  #9
                  Re: Combobox - How to match item index with record ID

                  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.D ataSource. 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.dWF SelectView(dSQL )

                  cbServiceCode.D ataSource = dS

                  cbServiceCode.D isplayMember = "oxyservicecode "

                  cbServiceCode.V alueMember = "oxyservice id"

                  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.dWF SelectView(dSQL )

                  ' MsgBox(dS.Table s(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).Ro ws

                  mServiceCode = Trim(dSRow.Item ("oxyServiceCod e"))

                  mServiceDesc = Trim(dSRow.Item ("oxyServiceDes c"))

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

                  mServiceArray.A dd(New AC_DataBoss.AC_ DisplayList(mSe rviceCode, mServiceID))

                  'cbServiceCode. Items.Add(m)

                  Next

                  cbServiceCode.D ataSource = mServiceArray

                  cbServiceCode.D isplayMember = "LongName"

                  cbServiceCode.V alueMember = "ShortName"

                  End Function





                  "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                  news:uZo4H02XFH A.612@TK2MSFTNG P12.phx.gbl...[color=blue]
                  > Bill,
                  >[color=green]
                  >>
                  >> However, the combobox somehow display the Valuemember instead of
                  >> DisplayMember!
                  >> What did I do wrong?
                  >>[/color]
                  >
                  > Can you show that piece of code frome you,
                  >
                  > Cor
                  >[/color]


                  Comment

                  • Cor Ligthert

                    #10
                    Re: Combobox - How to match item index with record ID

                    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.D ataSource = dS.Tables(0)

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

                    I hope it helps,

                    Cor


                    Comment

                    • Bill Nguyen

                      #11
                      Re: Combobox - How to match item index with record ID

                      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" <notmyfirstname @planet.nl> wrote in message
                      news:u5EFfcTYFH A.1204@TK2MSFTN GP10.phx.gbl...[color=blue]
                      > 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.D ataSource = dS.Tables(0)
                      >
                      > I stopped with reading when I saw this by the way.
                      >
                      > I hope it helps,
                      >
                      > Cor
                      >[/color]


                      Comment

                      • stand__sure

                        #12
                        Re: Combobox - How to match item index with record ID

                        Bill Nguyen wrote:[color=blue]
                        > I got displaying problem after that, but it was the order and
                        > case-sensitive issue with the column names.[/color]

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

                        Comment

                        Working...