This is a migrated thread and some comments may be shown as answers.

Databound dropdownList set selecteditem to nothing

3 Answers 213 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Marco
Top achievements
Rank 2
Veteran
Marco asked on 05 Mar 2013, 05:54 PM
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.

3 Answers, 1 is accepted

Sort by
0
Accepted
Peter
Telerik team
answered on 07 Mar 2013, 04:30 PM
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.
0
Marco
Top achievements
Rank 2
Veteran
answered on 08 Mar 2013, 09:40 AM
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
0
Peter
Telerik team
answered on 11 Mar 2013, 03:43 PM
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.
Tags
DropDownList
Asked by
Marco
Top achievements
Rank 2
Veteran
Answers by
Peter
Telerik team
Marco
Top achievements
Rank 2
Veteran
Share this question
or