Programatically changing value of databound combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Verona

    Programatically changing value of databound combobox

    I posted this in dotnet.language s.vb.controls but thought I'd post here as
    well..


    I have a combobox that is bound to a dataview generated from a dataset.

    The dataset has a single table (called "Data") with two columns "Id" and
    "Descriptio n". Id contains a code and description contains the
    description that is displayed in the combobox.

    The dataview is generated from the dataset using dv =
    ds.Tables("Data ").DefaultV iew.

    The Combobox is bound using :

    txtLookup.DataS ource = dv
    txtLookup.Value Member = "Id"

    txtLookup.Displ ayMember = "Descriptio n"



    This works fine, I can read the current "Id" from the combobox using the
    txtlookup.selec tedvalue property.

    However, if I want to programatically set the starting value of the
    combobox, I can't seem to work out how to do it. I thought I could just set
    the selectedvalue property to any of the vaules of "id" and the combobox
    would then display the corresponding description. This appears not to be
    the case. I don't know if it is getting confused by the fact that the "Id"
    field in the datatable is numeric or if I'm just doing it wrong!

    What should I be doing?



    Thanks in advance

    Simon


  • Mr Newbie

    #2
    Re: Programatically changing value of databound combobox

    Combo box index are Zero based and incremental. Your ID column does not have
    to correspond with this at all and can be any unique numbers. And yes,
    selecting a dropdownlist item is done by setting the SelectedItemInd ex to an
    appropriate number.


    "Simon Verona" <nomail@nomail. zzz> wrote in message
    news:%23ZbthpOx FHA.2076@TK2MSF TNGP14.phx.gbl. ..[color=blue]
    >I posted this in dotnet.language s.vb.controls but thought I'd post here as
    >well..
    >
    >
    > I have a combobox that is bound to a dataview generated from a dataset.
    >
    > The dataset has a single table (called "Data") with two columns "Id" and
    > "Descriptio n". Id contains a code and description contains the
    > description that is displayed in the combobox.
    >
    > The dataview is generated from the dataset using dv =
    > ds.Tables("Data ").DefaultV iew.
    >
    > The Combobox is bound using :
    >
    > txtLookup.DataS ource = dv
    > txtLookup.Value Member = "Id"
    >
    > txtLookup.Displ ayMember = "Descriptio n"
    >
    >
    >
    > This works fine, I can read the current "Id" from the combobox using the
    > txtlookup.selec tedvalue property.
    >
    > However, if I want to programatically set the starting value of the
    > combobox, I can't seem to work out how to do it. I thought I could just
    > set the selectedvalue property to any of the vaules of "id" and the
    > combobox would then display the corresponding description. This appears
    > not to be the case. I don't know if it is getting confused by the fact
    > that the "Id" field in the datatable is numeric or if I'm just doing it
    > wrong!
    >
    > What should I be doing?
    >
    >
    >
    > Thanks in advance
    >
    > Simon
    >
    >[/color]


    Comment

    • Simon Verona

      #3
      Re: Programatically changing value of databound combobox

      But how do I know what the appropriate no is ???

      If my datatable has for example:

      Id Description
      1 Ford
      2 Vauxhall
      3 Peugeot
      6 Ferrari

      and I want to set the combobox to "Ferrari" (ie ID=6).. How do I do this?

      I'm trying to do : txtlookup.selec tedvalue="6" but this isn't doing
      what I expect!

      I presume that the selecteditemind ex property is an offset through the
      datatable. Do I have to scan through the datatable counting how far through
      the record I want actually is? Will it matter that I've set the combobox
      to sort by Description??

      Regards
      Simon
      "Mr Newbie" <here@now.com > wrote in message
      news:OPfrfcPxFH A.700@TK2MSFTNG P11.phx.gbl...[color=blue]
      > Combo box index are Zero based and incremental. Your ID column does not
      > have to correspond with this at all and can be any unique numbers. And
      > yes, selecting a dropdownlist item is done by setting the
      > SelectedItemInd ex to an appropriate number.
      >
      >
      > "Simon Verona" <nomail@nomail. zzz> wrote in message
      > news:%23ZbthpOx FHA.2076@TK2MSF TNGP14.phx.gbl. ..[color=green]
      >>I posted this in dotnet.language s.vb.controls but thought I'd post here as
      >>well..
      >>
      >>
      >> I have a combobox that is bound to a dataview generated from a dataset.
      >>
      >> The dataset has a single table (called "Data") with two columns "Id" and
      >> "Descriptio n". Id contains a code and description contains the
      >> description that is displayed in the combobox.
      >>
      >> The dataview is generated from the dataset using dv =
      >> ds.Tables("Data ").DefaultV iew.
      >>
      >> The Combobox is bound using :
      >>
      >> txtLookup.DataS ource = dv
      >> txtLookup.Value Member = "Id"
      >>
      >> txtLookup.Displ ayMember = "Descriptio n"
      >>
      >>
      >>
      >> This works fine, I can read the current "Id" from the combobox using the
      >> txtlookup.selec tedvalue property.
      >>
      >> However, if I want to programatically set the starting value of the
      >> combobox, I can't seem to work out how to do it. I thought I could just
      >> set the selectedvalue property to any of the vaules of "id" and the
      >> combobox would then display the corresponding description. This appears
      >> not to be the case. I don't know if it is getting confused by the fact
      >> that the "Id" field in the datatable is numeric or if I'm just doing it
      >> wrong!
      >>
      >> What should I be doing?
      >>
      >>
      >>
      >> Thanks in advance
      >>
      >> Simon
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Bart Mermuys

        #4
        Re: Programatically changing value of databound combobox

        Hi,

        "Simon Verona" <nomail@nomail. zzz> wrote in message
        news:uGQA40PxFH A.1168@TK2MSFTN GP15.phx.gbl...[color=blue]
        > But how do I know what the appropriate no is ???
        >
        > If my datatable has for example:
        >
        > Id Description
        > 1 Ford
        > 2 Vauxhall
        > 3 Peugeot
        > 6 Ferrari
        >
        > and I want to set the combobox to "Ferrari" (ie ID=6).. How do I do this?
        >
        > I'm trying to do : txtlookup.selec tedvalue="6" but this isn't doing
        > what I expect![/color]

        You need to know the datatype of the "ValueMembe r" column and if it is by
        example an integer and not a string then you would use:

        txtlookup.Selec tedValue = 6 ' without quotes


        HTH,
        Greetings

        [color=blue]
        >
        > I presume that the selecteditemind ex property is an offset through the
        > datatable. Do I have to scan through the datatable counting how far
        > through the record I want actually is? Will it matter that I've set the
        > combobox to sort by Description??
        >
        > Regards
        > Simon
        > "Mr Newbie" <here@now.com > wrote in message
        > news:OPfrfcPxFH A.700@TK2MSFTNG P11.phx.gbl...[color=green]
        >> Combo box index are Zero based and incremental. Your ID column does not
        >> have to correspond with this at all and can be any unique numbers. And
        >> yes, selecting a dropdownlist item is done by setting the
        >> SelectedItemInd ex to an appropriate number.
        >>
        >>
        >> "Simon Verona" <nomail@nomail. zzz> wrote in message
        >> news:%23ZbthpOx FHA.2076@TK2MSF TNGP14.phx.gbl. ..[color=darkred]
        >>>I posted this in dotnet.language s.vb.controls but thought I'd post here
        >>>as well..
        >>>
        >>>
        >>> I have a combobox that is bound to a dataview generated from a dataset.
        >>>
        >>> The dataset has a single table (called "Data") with two columns "Id"
        >>> and "Descriptio n". Id contains a code and description contains the
        >>> description that is displayed in the combobox.
        >>>
        >>> The dataview is generated from the dataset using dv =
        >>> ds.Tables("Data ").DefaultV iew.
        >>>
        >>> The Combobox is bound using :
        >>>
        >>> txtLookup.DataS ource = dv
        >>> txtLookup.Value Member = "Id"
        >>>
        >>> txtLookup.Displ ayMember = "Descriptio n"
        >>>
        >>>
        >>>
        >>> This works fine, I can read the current "Id" from the combobox using the
        >>> txtlookup.selec tedvalue property.
        >>>
        >>> However, if I want to programatically set the starting value of the
        >>> combobox, I can't seem to work out how to do it. I thought I could just
        >>> set the selectedvalue property to any of the vaules of "id" and the
        >>> combobox would then display the corresponding description. This appears
        >>> not to be the case. I don't know if it is getting confused by the fact
        >>> that the "Id" field in the datatable is numeric or if I'm just doing it
        >>> wrong!
        >>>
        >>> What should I be doing?
        >>>
        >>>
        >>>
        >>> Thanks in advance
        >>>
        >>> Simon
        >>>
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Simon Verona

          #5
          Re: Programatically changing value of databound combobox

          I must be doing something very wrong because that doesn't help!

          Even though the "Id" field contains numerics, it is defined as a string
          field.

          Regards
          Simon
          "Bart Mermuys" <bmermuys.nospa m@hotmail.com> wrote in message
          news:ebVvpYQxFH A.3300@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Hi,
          >
          > "Simon Verona" <nomail@nomail. zzz> wrote in message
          > news:uGQA40PxFH A.1168@TK2MSFTN GP15.phx.gbl...[color=green]
          >> But how do I know what the appropriate no is ???
          >>
          >> If my datatable has for example:
          >>
          >> Id Description
          >> 1 Ford
          >> 2 Vauxhall
          >> 3 Peugeot
          >> 6 Ferrari
          >>
          >> and I want to set the combobox to "Ferrari" (ie ID=6).. How do I do this?
          >>
          >> I'm trying to do : txtlookup.selec tedvalue="6" but this isn't doing
          >> what I expect![/color]
          >
          > You need to know the datatype of the "ValueMembe r" column and if it is by
          > example an integer and not a string then you would use:
          >
          > txtlookup.Selec tedValue = 6 ' without quotes
          >
          >
          > HTH,
          > Greetings
          >
          >[color=green]
          >>
          >> I presume that the selecteditemind ex property is an offset through the
          >> datatable. Do I have to scan through the datatable counting how far
          >> through the record I want actually is? Will it matter that I've set the
          >> combobox to sort by Description??
          >>
          >> Regards
          >> Simon
          >> "Mr Newbie" <here@now.com > wrote in message
          >> news:OPfrfcPxFH A.700@TK2MSFTNG P11.phx.gbl...[color=darkred]
          >>> Combo box index are Zero based and incremental. Your ID column does not
          >>> have to correspond with this at all and can be any unique numbers. And
          >>> yes, selecting a dropdownlist item is done by setting the
          >>> SelectedItemInd ex to an appropriate number.
          >>>
          >>>
          >>> "Simon Verona" <nomail@nomail. zzz> wrote in message
          >>> news:%23ZbthpOx FHA.2076@TK2MSF TNGP14.phx.gbl. ..
          >>>>I posted this in dotnet.language s.vb.controls but thought I'd post here
          >>>>as well..
          >>>>
          >>>>
          >>>> I have a combobox that is bound to a dataview generated from a dataset.
          >>>>
          >>>> The dataset has a single table (called "Data") with two columns "Id"
          >>>> and "Descriptio n". Id contains a code and description contains the
          >>>> description that is displayed in the combobox.
          >>>>
          >>>> The dataview is generated from the dataset using dv =
          >>>> ds.Tables("Data ").DefaultV iew.
          >>>>
          >>>> The Combobox is bound using :
          >>>>
          >>>> txtLookup.DataS ource = dv
          >>>> txtLookup.Value Member = "Id"
          >>>>
          >>>> txtLookup.Displ ayMember = "Descriptio n"
          >>>>
          >>>>
          >>>>
          >>>> This works fine, I can read the current "Id" from the combobox using
          >>>> the txtlookup.selec tedvalue property.
          >>>>
          >>>> However, if I want to programatically set the starting value of the
          >>>> combobox, I can't seem to work out how to do it. I thought I could
          >>>> just set the selectedvalue property to any of the vaules of "id" and
          >>>> the combobox would then display the corresponding description. This
          >>>> appears not to be the case. I don't know if it is getting confused by
          >>>> the fact that the "Id" field in the datatable is numeric or if I'm just
          >>>> doing it wrong!
          >>>>
          >>>> What should I be doing?
          >>>>
          >>>>
          >>>>
          >>>> Thanks in advance
          >>>>
          >>>> Simon
          >>>>
          >>>>
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Mr Newbie

            #6
            Re: Programatically changing value of databound combobox

            OK,

            'For example, a delete operation

            datasetMyVehicl es.CarsTable.Ro ws.Find(
            CarValueFromCom boBoxInThisCase ItIs_6 ).Delete()

            myDataAdapter.U pdate( datasetMyVehicl es.CarsTable )


            HTH


            "Simon Verona" <nomail@nomail. zzz> wrote in message
            news:%23F5Pw6Qx FHA.720@TK2MSFT NGP15.phx.gbl.. .[color=blue]
            >I must be doing something very wrong because that doesn't help!
            >
            > Even though the "Id" field contains numerics, it is defined as a string
            > field.
            >
            > Regards
            > Simon
            > "Bart Mermuys" <bmermuys.nospa m@hotmail.com> wrote in message
            > news:ebVvpYQxFH A.3300@TK2MSFTN GP09.phx.gbl...[color=green]
            >> Hi,
            >>
            >> "Simon Verona" <nomail@nomail. zzz> wrote in message
            >> news:uGQA40PxFH A.1168@TK2MSFTN GP15.phx.gbl...[color=darkred]
            >>> But how do I know what the appropriate no is ???
            >>>
            >>> If my datatable has for example:
            >>>
            >>> Id Description
            >>> 1 Ford
            >>> 2 Vauxhall
            >>> 3 Peugeot
            >>> 6 Ferrari
            >>>
            >>> and I want to set the combobox to "Ferrari" (ie ID=6).. How do I do
            >>> this?
            >>>
            >>> I'm trying to do : txtlookup.selec tedvalue="6" but this isn't
            >>> doing what I expect![/color]
            >>
            >> You need to know the datatype of the "ValueMembe r" column and if it is by
            >> example an integer and not a string then you would use:
            >>
            >> txtlookup.Selec tedValue = 6 ' without quotes
            >>
            >>
            >> HTH,
            >> Greetings
            >>
            >>[color=darkred]
            >>>
            >>> I presume that the selecteditemind ex property is an offset through the
            >>> datatable. Do I have to scan through the datatable counting how far
            >>> through the record I want actually is? Will it matter that I've set
            >>> the combobox to sort by Description??
            >>>
            >>> Regards
            >>> Simon
            >>> "Mr Newbie" <here@now.com > wrote in message
            >>> news:OPfrfcPxFH A.700@TK2MSFTNG P11.phx.gbl...
            >>>> Combo box index are Zero based and incremental. Your ID column does not
            >>>> have to correspond with this at all and can be any unique numbers. And
            >>>> yes, selecting a dropdownlist item is done by setting the
            >>>> SelectedItemInd ex to an appropriate number.
            >>>>
            >>>>
            >>>> "Simon Verona" <nomail@nomail. zzz> wrote in message
            >>>> news:%23ZbthpOx FHA.2076@TK2MSF TNGP14.phx.gbl. ..
            >>>>>I posted this in dotnet.language s.vb.controls but thought I'd post here
            >>>>>as well..
            >>>>>
            >>>>>
            >>>>> I have a combobox that is bound to a dataview generated from a
            >>>>> dataset.
            >>>>>
            >>>>> The dataset has a single table (called "Data") with two columns "Id"
            >>>>> and "Descriptio n". Id contains a code and description contains the
            >>>>> description that is displayed in the combobox.
            >>>>>
            >>>>> The dataview is generated from the dataset using dv =
            >>>>> ds.Tables("Data ").DefaultV iew.
            >>>>>
            >>>>> The Combobox is bound using :
            >>>>>
            >>>>> txtLookup.DataS ource = dv
            >>>>> txtLookup.Value Member = "Id"
            >>>>>
            >>>>> txtLookup.Displ ayMember = "Descriptio n"
            >>>>>
            >>>>>
            >>>>>
            >>>>> This works fine, I can read the current "Id" from the combobox using
            >>>>> the txtlookup.selec tedvalue property.
            >>>>>
            >>>>> However, if I want to programatically set the starting value of the
            >>>>> combobox, I can't seem to work out how to do it. I thought I could
            >>>>> just set the selectedvalue property to any of the vaules of "id" and
            >>>>> the combobox would then display the corresponding description. This
            >>>>> appears not to be the case. I don't know if it is getting confused by
            >>>>> the fact that the "Id" field in the datatable is numeric or if I'm
            >>>>> just doing it wrong!
            >>>>>
            >>>>> What should I be doing?
            >>>>>
            >>>>>
            >>>>>
            >>>>> Thanks in advance
            >>>>>
            >>>>> Simon
            >>>>>
            >>>>>
            >>>>
            >>>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Bart Mermuys

              #7
              Re: Programatically changing value of databound combobox

              Hi,

              "Simon Verona" <nomail@nomail. zzz> wrote in message
              news:%23F5Pw6Qx FHA.720@TK2MSFT NGP15.phx.gbl.. .[color=blue]
              >I must be doing something very wrong because that doesn't help!
              >
              > Even though the "Id" field contains numerics, it is defined as a string
              > field.[/color]

              It would be strange that a numeric field would have a string datatype but it
              shouldn't matter for the ComboBox, as long as you use the same type with
              SelectedValue.

              Check the column type (to be sure):
              DataTable.Colum ns("id").DataTy pe.ToString()

              The ComboBox is visible and you are setting SelectedValue after the ComboBox
              is bound, right ?


              greetings
              [color=blue]
              >
              > Regards
              > Simon
              > "Bart Mermuys" <bmermuys.nospa m@hotmail.com> wrote in message
              > news:ebVvpYQxFH A.3300@TK2MSFTN GP09.phx.gbl...[color=green]
              >> Hi,
              >>
              >> "Simon Verona" <nomail@nomail. zzz> wrote in message
              >> news:uGQA40PxFH A.1168@TK2MSFTN GP15.phx.gbl...[color=darkred]
              >>> But how do I know what the appropriate no is ???
              >>>
              >>> If my datatable has for example:
              >>>
              >>> Id Description
              >>> 1 Ford
              >>> 2 Vauxhall
              >>> 3 Peugeot
              >>> 6 Ferrari
              >>>
              >>> and I want to set the combobox to "Ferrari" (ie ID=6).. How do I do
              >>> this?
              >>>
              >>> I'm trying to do : txtlookup.selec tedvalue="6" but this isn't
              >>> doing what I expect![/color]
              >>
              >> You need to know the datatype of the "ValueMembe r" column and if it is by
              >> example an integer and not a string then you would use:
              >>
              >> txtlookup.Selec tedValue = 6 ' without quotes
              >>
              >>
              >> HTH,
              >> Greetings
              >>
              >>[color=darkred]
              >>>
              >>> I presume that the selecteditemind ex property is an offset through the
              >>> datatable. Do I have to scan through the datatable counting how far
              >>> through the record I want actually is? Will it matter that I've set
              >>> the combobox to sort by Description??
              >>>
              >>> Regards
              >>> Simon
              >>> "Mr Newbie" <here@now.com > wrote in message
              >>> news:OPfrfcPxFH A.700@TK2MSFTNG P11.phx.gbl...
              >>>> Combo box index are Zero based and incremental. Your ID column does not
              >>>> have to correspond with this at all and can be any unique numbers. And
              >>>> yes, selecting a dropdownlist item is done by setting the
              >>>> SelectedItemInd ex to an appropriate number.
              >>>>
              >>>>
              >>>> "Simon Verona" <nomail@nomail. zzz> wrote in message
              >>>> news:%23ZbthpOx FHA.2076@TK2MSF TNGP14.phx.gbl. ..
              >>>>>I posted this in dotnet.language s.vb.controls but thought I'd post here
              >>>>>as well..
              >>>>>
              >>>>>
              >>>>> I have a combobox that is bound to a dataview generated from a
              >>>>> dataset.
              >>>>>
              >>>>> The dataset has a single table (called "Data") with two columns "Id"
              >>>>> and "Descriptio n". Id contains a code and description contains the
              >>>>> description that is displayed in the combobox.
              >>>>>
              >>>>> The dataview is generated from the dataset using dv =
              >>>>> ds.Tables("Data ").DefaultV iew.
              >>>>>
              >>>>> The Combobox is bound using :
              >>>>>
              >>>>> txtLookup.DataS ource = dv
              >>>>> txtLookup.Value Member = "Id"
              >>>>>
              >>>>> txtLookup.Displ ayMember = "Descriptio n"
              >>>>>
              >>>>>
              >>>>>
              >>>>> This works fine, I can read the current "Id" from the combobox using
              >>>>> the txtlookup.selec tedvalue property.
              >>>>>
              >>>>> However, if I want to programatically set the starting value of the
              >>>>> combobox, I can't seem to work out how to do it. I thought I could
              >>>>> just set the selectedvalue property to any of the vaules of "id" and
              >>>>> the combobox would then display the corresponding description. This
              >>>>> appears not to be the case. I don't know if it is getting confused by
              >>>>> the fact that the "Id" field in the datatable is numeric or if I'm
              >>>>> just doing it wrong!
              >>>>>
              >>>>> What should I be doing?
              >>>>>
              >>>>>
              >>>>>
              >>>>> Thanks in advance
              >>>>>
              >>>>> Simon
              >>>>>
              >>>>>
              >>>>
              >>>>
              >>>
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Simon Verona

                #8
                Re: Programatically changing value of databound combobox

                Just to complete this thread, there was nothing actually wrong with the code
                I had, the selectvalue works just fine. My problem was actually completely
                unrelated to the combobox but was a corruption of the value that I was
                setting the selectedvalue to!

                Sometimes yuo just don't see the wood for the trees!!

                Thanks

                Regards
                Simon
                "Simon Verona" <nomail@nomail. zzz> wrote in message
                news:%23ZbthpOx FHA.2076@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                >I posted this in dotnet.language s.vb.controls but thought I'd post here as
                >well..
                >
                >
                > I have a combobox that is bound to a dataview generated from a dataset.
                >
                > The dataset has a single table (called "Data") with two columns "Id" and
                > "Descriptio n". Id contains a code and description contains the
                > description that is displayed in the combobox.
                >
                > The dataview is generated from the dataset using dv =
                > ds.Tables("Data ").DefaultV iew.
                >
                > The Combobox is bound using :
                >
                > txtLookup.DataS ource = dv
                > txtLookup.Value Member = "Id"
                >
                > txtLookup.Displ ayMember = "Descriptio n"
                >
                >
                >
                > This works fine, I can read the current "Id" from the combobox using the
                > txtlookup.selec tedvalue property.
                >
                > However, if I want to programatically set the starting value of the
                > combobox, I can't seem to work out how to do it. I thought I could just
                > set the selectedvalue property to any of the vaules of "id" and the
                > combobox would then display the corresponding description. This appears
                > not to be the case. I don't know if it is getting confused by the fact
                > that the "Id" field in the datatable is numeric or if I'm just doing it
                > wrong!
                >
                > What should I be doing?
                >
                >
                >
                > Thanks in advance
                >
                > Simon
                >
                >[/color]


                Comment

                Working...