WinForms - ComboBox:查找所选项目并设置索引

时间:2011-11-17 22:07:34

标签: c# winforms combobox telerik

我有一个RadMultiColumnComboBox类型的ComboBox控件,我正在尝试搜索并查找字符串,然后以编程方式设置所选索引。

这是我的代码:

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

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

问题是控件FindExact方法返回-1而不管我传入FindExact的字符串是什么。

当应用程序运行时,我使用立即窗口测试并输入各种字符串;无论我使用什么字符串,它都返回-1。

如果我检查myComboBox,DataSource属性中有10个项目。

以下是ComboBox的表示 - 它可能有所帮助:

enter image description here

1 个答案:

答案 0 :(得分:4)

您可以将数据源转换为原始类型并从DataSource中查找索引:

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