combo problem

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

    #1

    combo problem

    hi i have a combobox which has some values
    like (eg)
    a
    b
    c
    d


    i want to retrieve the value from the db (a, or b or c or d)
    and then select that item in the combo according ly

    foreach (DataRow dr in recs)
    {
    cmbType.Selecte dText = dr["descriptio n"].ToString();
    }

    but this doesnt work
    dr["descriptio n"] has the correct value i want to select in the combo


  • Jeff Molby

    #2
    Re: combo problem

    I believe the SelectedText property returns the description of the current
    item. I think what you want is

    cmbType.items.a dd(dr["descriptio n"].ToString());


    "drishtik" <sheraze@hotmai l.com> wrote in message
    news:OoilGvEfDH A.2484@TK2MSFTN GP09.phx.gbl...[color=blue]
    > hi i have a combobox which has some values
    > like (eg)
    > a
    > b
    > c
    > d
    >
    >
    > i want to retrieve the value from the db (a, or b or c or d)
    > and then select that item in the combo according ly
    >
    > foreach (DataRow dr in recs)
    > {
    > cmbType.Selecte dText = dr["descriptio n"].ToString();
    > }
    >
    > but this doesnt work
    > dr["descriptio n"] has the correct value i want to select in the combo
    >
    >[/color]


    Comment

    • drishtik

      #3
      Re: combo problem

      hi all the items are already loaded in the combo box.
      if the query from the database returns a "b"
      i want to select b in the combo.
      thanx


      Comment

      • Jeff Molby

        #4
        Re: combo problem

        Sorry, I misunderstood.

        Why is your statement in a foreach loop? How many records are in your
        dataset? If there is more than one, wouldn't they all just be overwritten
        except for the last one?
        "drishtik" <sheraze@hotmai l.com> wrote in message
        news:OjmkeMFfDH A.3024@tk2msftn gp13.phx.gbl...[color=blue]
        > hi all the items are already loaded in the combo box.
        > if the query from the database returns a "b"
        > i want to select b in the combo.
        > thanx
        >
        >[/color]


        Comment

        • drishtik

          #5
          Re: combo problem

          it has only 1 record.



          "Jeff Molby" <JeffMolby@C_mc _st.n_t> wrote in message
          news:uDA8nJGfDH A.3248@tk2msftn gp13.phx.gbl...[color=blue]
          > Sorry, I misunderstood.
          >
          > Why is your statement in a foreach loop? How many records are in your
          > dataset? If there is more than one, wouldn't they all just be overwritten
          > except for the last one?
          > "drishtik" <sheraze@hotmai l.com> wrote in message
          > news:OjmkeMFfDH A.3024@tk2msftn gp13.phx.gbl...[color=green]
          > > hi all the items are already loaded in the combo box.
          > > if the query from the database returns a "b"
          > > i want to select b in the combo.
          > > thanx
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Jeff Molby

            #6
            Re: combo problem

            Are there many items in your combo? If not, I'd just loop through everything
            in the combo.Items collection. When you find the one that matches, set the
            Selected property of the Item = true.

            If there are a lot of items, this is probably inefficient. In that case, I'd
            try using the SelectedValue property.

            If that doesn't work, post the rest of your code and I'll see if I can
            figure out what's going wrong.

            "drishtik" <sheraze@hotmai l.com> wrote in message
            news:OzGEYjFfDH A.2748@TK2MSFTN GP11.phx.gbl...[color=blue]
            > thts exactly what i tried but it doesnt get selected. and always gives a
            > null
            >
            > cmbType.Selecte dText = dr["descriptio n"].ToString();
            >
            >[/color]


            Comment

            • drishtik

              #7
              Re: combo problem

              hi thanx for the reply

              id like to use the selectedvalue property too

              dsCar = ser.getAllCars( );
              DataRow[] recs = dsCar.Tables[0].Select("Carid = " +
              txCarID.Text.To String());
              foreach (DataRow dr in recs) //if i have only 1 record, i dont need this
              loop but how do i get the value in recs?
              {
              cmbType.Selecte dValue = dr["descriptio n"].ToString();
              cmbCars.Selecte dValue = dr["name"].ToString();
              }

              also how do i loop thru all the items in a combo?

              thanx


              Comment

              • Jeff Molby

                #8
                Re: combo problem

                foreach (ListItem li in cmbType.Items)
                {
                if (li.Text = dr["descriptio n"].ToString())
                {li.Selected = true;}
                }

                I'm mostly a VB developer, so my syntax may be crap, but hopefully you get
                the idea.

                "drishtik" <sheraze@hotmai l.com> wrote in message
                news:OD0l32lfDH A.2328@TK2MSFTN GP09.phx.gbl...[color=blue]
                > hi thanx for the reply
                >
                > id like to use the selectedvalue property too
                >
                > dsCar = ser.getAllCars( );
                > DataRow[] recs = dsCar.Tables[0].Select("Carid = " +
                > txCarID.Text.To String());
                > foreach (DataRow dr in recs) //if i have only 1 record, i dont need this
                > loop but how do i get the value in recs?
                > {
                > cmbType.Selecte dValue = dr["descriptio n"].ToString();
                > cmbCars.Selecte dValue = dr["name"].ToString();
                > }
                >
                > also how do i loop thru all the items in a combo?
                >
                > thanx
                >
                >[/color]


                Comment

                Working...