Databound dropdownList set selecteditem to nothing

4 posts, 1 answers
  1. Marco
    Marco avatar
    87 posts
    Member since:
    Apr 2011

    Posted 05 Mar 2013 Link to this post

    Hello

    With the legacy Winforms combobox (databound mode), when you enter a text which do not correspond to an item of the datasource the selectedIndex property of the combobox is -1 and the selectedItem is Nothing.

    With the radDropDownList, I have the same behavior if I never have a valid item selected. But when an item has been selected and you type a "wrong" text, the selectedIndex and selectedItem always return the last selected index instead of -1 and Nothing.

    So I can get in this kind of situation:

    The displayed text is dirty (not corresponding to a valid value of my list) but the selected Item is a valid (and unknow) value !

    Is there a way to fix this issue? I don't want to force the users to choose a valid value because I like to consider that's invalid text (most of time a blank one (If not I set it blank on validation)) with SelectedIndex = -1 and SelectedItem Is Nothing  is for a Null Value.
  2. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 07 Mar 2013 Link to this post

    Hello,

    Thank you for writing.

    RadDropDownList does not support such functionality because this is a time consuming operation - search for a match in all items

    A possible approach is to handle the TextChanged event and set the SelectedIndex if the text is found in the items. For example:
    int SelectedIndex = this.radDropDownList1.SelectedIndex;
    private void radDropDownList1_TextChanged(object sender, EventArgs e)
    {
              RadListDataItem item = this.radDropDownList1.FindItemExact(this.radDropDownList1.Text, false);
              if( item!=null)
              {
                  SelectedIndex =item.RowIndex;
              }
    }

    Another approach is to use the RadDropDownList.DropDownListElement.SelectedIndex property which is synchronized with the text in the editable portion of the DropDownList.

    I hope this helps. All the best,
    Peter
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Marco
    Marco avatar
    87 posts
    Member since:
    Apr 2011

    Posted 08 Mar 2013 Link to this post

    Hello Peter,

    I have tried the RadDropDownList.DropDownListElement.SelectedIndex approach and it solved my little issue.

    Now when the dropdownlist is validating, If the entered text is not corresponding to an element of the list, selectedItem and text are cleared.

    Thank you !

     

    Private Sub MyRadDropDownList_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyRadDropDownList.Validating
      
    Dim aDropDownControl As RadDropDownList = TryCast(sender, RadDropDownList)
      
    If aDropDownControl Is Nothing Then Exit Sub
       
    If aDropDownControl.DropDownListElement.SelectedIndex = -1 Then
      
     aDropDownControl.SelectedItem = Nothing
      
     aDropDownControl.Text = ""
      
    End If
      
     
     
    End Sub
  5. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 11 Mar 2013 Link to this post

    Hi Marco,

    Thank you for sharing your solution with us. Do not hesitate to write back with any further questions.

    Greetings,
    Peter
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Back to Top