SelectedValue not set in DropDownList

2 posts, 0 answers
  1. Jonathan Zee
    Jonathan Zee avatar
    22 posts
    Member since:
    Mar 2009

    Posted 30 Mar 2011 Link to this post

    Hi guys,

    I'm in the process of upgrading the previous RadComboBox from 2009 to the newest version and I encountered a weird behaviour with the new RadDropDownList. ( note: this also affects RadComboBox in the newest version)

    I did a simple test by binding the controls to a strongly typed datatable, setting the valuemembers and display members and then set the default value to 1 of the items that exist in that table. -

    Listed below are code snippets from the old RadComboBox (2009) and the new RadDropDownList.
     
    2009 version of the code which works. - The end result of this is that when the combobox is loaded, the default selected value is Australia. If I just refactored the code by changing this RadComboBox to a RadDropDownList and try to Debug it right after the box.SelectedValue = "Au"; shows that the selectedValue is still null.

    RadComboBox box = new RadComboBox();
     box.ValueMember = "Code";
     box.DisplayMember = "Name";
     box.DataSource = [StronglyTypedCountryDataTable] // this table contains columns code, name
     box.SelectedValue = "Au";

    After upgrading to the new 2011 version, the above mentioned code doesnt work any more. when the control is loaded, it always displays the 1st item in the box ("Afghanistan") even when the selected value has been set to Au. I tried changing the datasource to a List, DataView and the same behaviour exists.

    Finally, I tried adding the items manually and it worked...

    2011 version -

    RadDropDownList list = new RadDropDownList();
    list.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
    Country country = Country.Get();
    list.ValueMember = "Code";
    list.DisplayMember = "Name";
     
    foreach (CountryBe.CountryRow row in country.BusinessEntity.Country)
    {
        list.Items.Add(new RadListDataItem(row.Name,row.Code));  
    }
     
    list.SelectedValue = "Au";

    Can someone explain to me why the 1st method of implementation doesnt work the same as the 2nd method?

  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 04 Apr 2011 Link to this post

    Hello Jonathan,

    Thank you for writing.

    I would recommend selecting an item as shown below:

    list.SelectedIndex = list.DropDownListElement.FindStringExact("Au");

    By default, RadDataSource causes RadListControl and RadDropDownList to select the first item by default, e.g. it sets the current position to 0 and this is a desired behavior. In case that you want to use the old approach, I suggest that you set the SelectedValue property:

     

    box.SelectedValue = "Au";

    after the form is loaded, because changing the Binding Context will select the first item.

    I hope this helps.

    Greetings,

    Peter
    the Telerik team
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top