DharaPPatel
DharaPPatel

Reputation: 12743

Selecting item from dropdownlist dynamically

I want to select an item dynamically from a database into a dropdownlist.
I can set value property of the dropdownlist but I am not able to set the item property, why so?
My database has text stored in it so I have to select the item from dropdownlist.

adding code of comment

  string strGetBooking = travService.SEL_TourManualBooking("", long.Parse(hdtmbid.Value));
  if (strGetBooking != "") 
  {
      DataSet ds = new DataSet(); 
      ds.ReadXml(new StringReader(strGetBooking)); 
      DataTable dt = ds.Tables[0];
      drpTour.SelectedItem = dt.Rows[0]["FKTBID"].ToString(); 
  }  

Upvotes: 0

Views: 2768

Answers (2)

Peru
Peru

Reputation: 2971

SelectedIndex -- > The index number of the selected item 
SelectedItem -->  The text of the selected item (Text And Value)
SelectedValue--> The value of the selected item 
Text-- > The value of the selected item 

Upvotes: 3

Prateek Singh
Prateek Singh

Reputation: 863

Try this -

    // dropDown = Your Dropdown
    //dbValue = your database value(String Value)
    dropDown.SelectedText = dbValue;

[EDIT]

You can do this in one other way -

dropDown.SelectedIndex = dropDown.Items.IndexOf(dropDown.Items.FindByText(dbValue));

Upvotes: 0

Related Questions