Adding the same element to ComboBox

2 posts, 0 answers
  1. Dandan
    Dandan avatar
    17 posts
    Member since:
    Mar 2009

    Posted 07 May 2009 Link to this post

    Hi,

    I defined RadComboBox with duplicate elements, but when the user select an item, then the combo display (as the choosen item) the following item instead the selected item !!!

    The Code:
       

    RadComboBox1.Height = 25;

    RadComboBox1.Items.Add(

    "dana");

     

    RadComboBox1.Items.Add(

    "miriam");

     

    RadComboBox1.Items.Add(

    "aviad");

     

    RadComboBox1.Items.Add(

    "batya");

     

    RadComboBox1.Items.Add(

    "dana");

     

    RadComboBox1.Items.Add(

    "miri");

     

    RadComboBox1.Items.Add(

    "chaya");

     


    --> Run the application, then choose "miriam" - You get "Aviad"...

    Please how can i fix the problem???

    Thanks,
    Dana
  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 07 May 2009 Link to this post

    Hello Dana,

    This is a known limitation of RadComboBox and most items controls. To workaround it you have two choices:

    1) Instead of adding strings to the Items collection, add RadComboBoxItem controls:
    RadComboBox1.Items.Add(new RadComboBoxItem() { Content = "item1" });
    ...

    2) Instead of adding strings to the Items collection add custom objects and use DisplayMemberPath or ItemTemplate properties of RadComboBox:
    RadComboBox1.DisplayMemberPath = "Text";
    RadComboBox1.Items.Add(new DataItem() { Text = "item1" });
    ...

    I would recommend using the second option, since you have more control over the data objects and you could add additional data to each combobox item.

    The limitation is caused by the fact that the strings are special objects, that are compared by value and the combo box cannot differentiate the items, hence cannot properly calculate their indices when selecting.

    Regards,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top