combo problem

  • Thread starter Thread starter drishtik
  • Start date Start date
D

drishtik

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
 
I believe the SelectedText property returns the description of the current
item. I think what you want is

cmbType.items.add(dr["description"].ToString());
 
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
 
drishtik,

You can use the SelectedText property to set which item is selected.
So, if the query from the database returns B, then you can set the text to
that, and it should have the proper item selected.

Hope this helps.
 
thts exactly what i tried but it doesnt get selected. and always gives a
null

cmbType.SelectedText = dr["description"].ToString();
 
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?
 
it has only 1 record.



Jeff Molby said:
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?
 
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.
 
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
 
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.
 
Back
Top