NOCARRIER
NOCARRIER

Reputation: 2634

WinForms - ComboBox: Find selected item and set index

I have a ComboBox control of type RadMultiColumnComboBox and I'm trying to search and find a string and then set the selected index programmatically.

Here is my code:

        // get reference to drop down:
        RadMultiColumnComboBox myComboBox = this.BaseFieldControl;

        // find and set: 
        string toFind = "SomeValue";
        myComboBox.SelectedIndex = myComboBox .FindExact( toFind );

The problem is that the controls FindExact method is returning -1 not matter what string I pass into FindExact.

While the app is running I use the Immediate window to test and enter various strings; no matter what I string I use, it returns -1.

If I inspect myComboBox there are 10 items in the DataSource property.

Here is a representation of the ComboBox - it may help:

enter image description here

Upvotes: 0

Views: 9490

Answers (1)

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4908

you can cast datasource to original type and find index from DataSource:

var data=(List<YourType)myComboBox.DataSource;
myComboBox.SelectedIndex=data.FindIndex(p=>p.Text=="SomeValue");

Upvotes: 4

Related Questions