簡體   English   中英

如何從復選框列表控件的selectedValue獲取索引?

[英]How to get index from selectedvalue of a checkedlistbox control?

在Windows窗體中的其他控件中,我有一個CheckedListBox包含很多項目。用於填充CheckedListBox的代碼是:

Dictionary<string, string> ciDict = new Dictionary<string, string>();
ciDict.Add("1", "Audi");
ciDict.Add("2", "Suzuki");
ciDict.Add("3", "Saab");
ciDict.Add("4", "Tata");

clb.DataSource = new BindingSource(ciDict, null);
clb.DisplayMember = "Value";
clb.ValueMember = "Key";

當我將數據保存在表中時,我正在保存'ValueMember'.Now在上述表單的Edit模式下,我想使用之前保存的valuemember檢查CheckedListBox項目。我的問題是如何從中找到CheckedListBox項目的索引它的有價值的成員?希望您理解我的問題。

while (rdrCCA.Read())
{
   int index= clbCSA.Items.IndexOf(rdrCCA["CCA_ITEM_ID"]);
   clbCSA.SetItemChecked(index, true);
}

哪里

clbCSA= name of the checkedlistbox control
CCA_ITEM_ID = name of the table field where valumember are being stored.

此代碼無效。請提供一些建議。

由於您的數據在字典中,因此按值查找索引的最簡單方法是通過以下方式在字典中查找值的索引:

var index = yourDictionary.Keys.ToList().IndexOf("SomeValue");
if(index > -1)
    checkedListBox1.SetItemChecked(index, true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM