Connecting Tech Pros Worldwide Help | Site Map

combo problem

drishtik
Guest
 
Posts: n/a
#1: Nov 15 '05
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.SelectedText = dr["description"].ToString();
}

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


Jeff Molby
Guest
 
Posts: n/a
#2: Nov 15 '05

re: combo problem


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

cmbType.items.add(dr["description"].ToString());


"drishtik" <sheraze@hotmail.com> wrote in message
news:OoilGvEfDHA.2484@TK2MSFTNGP09.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.SelectedText = dr["description"].ToString();
> }
>
> but this doesnt work
> dr["description"] has the correct value i want to select in the combo
>
>[/color]


drishtik
Guest
 
Posts: n/a
#3: Nov 15 '05

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


Jeff Molby
Guest
 
Posts: n/a
#4: Nov 15 '05

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@hotmail.com> wrote in message
news:OjmkeMFfDHA.3024@tk2msftngp13.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]


drishtik
Guest
 
Posts: n/a
#5: Nov 15 '05

re: combo problem


it has only 1 record.



"Jeff Molby" <JeffMolby@C_mc_st.n_t> wrote in message
news:uDA8nJGfDHA.3248@tk2msftngp13.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@hotmail.com> wrote in message
> news:OjmkeMFfDHA.3024@tk2msftngp13.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]


Jeff Molby
Guest
 
Posts: n/a
#6: Nov 15 '05

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@hotmail.com> wrote in message
news:OzGEYjFfDHA.2748@TK2MSFTNGP11.phx.gbl...[color=blue]
> thts exactly what i tried but it doesnt get selected. and always gives a
> null
>
> cmbType.SelectedText = dr["description"].ToString();
>
>[/color]


drishtik
Guest
 
Posts: n/a
#7: Nov 15 '05

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.ToString());
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.SelectedValue = dr["description"].ToString();
cmbCars.SelectedValue = dr["name"].ToString();
}

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

thanx


Jeff Molby
Guest
 
Posts: n/a
#8: Nov 15 '05

re: combo problem


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

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

"drishtik" <sheraze@hotmail.com> wrote in message
news:OD0l32lfDHA.2328@TK2MSFTNGP09.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.ToString());
> 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.SelectedValue = dr["description"].ToString();
> cmbCars.SelectedValue = dr["name"].ToString();
> }
>
> also how do i loop thru all the items in a combo?
>
> thanx
>
>[/color]


Closed Thread