2011-11-17 91 views
0

我有型RadMultiColumnComboBox的ComboBox控件,我試圖尋找並找到一個字符串,然後設置所選指數編程。的WinForms - 組合框:查找所選的項目,設置索引

這裏是我的代碼:

 // 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

回答

4

你可以從數據源數據源投原始類型,發現指數:紅帽

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

感謝 - 我現在進行測試。 – nocarrier

+0

p.Text是一個你必須用p代替的例子(你的文件想要搜索)==「SomeData」 –

+0

我收到一個異常: {「無法轉換'System.ComponentModel.BindingList'類型的對象1 ECS.Common.Entities.MyType]爲鍵入」 System.Collections.Generic.List'1 [ECS.Common.Entities.MyType]'。「} – nocarrier