繁体   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