[英]How to get index from selectedvalue of a checkedlistbox control?
Among other controls in a windows form, i have a CheckedListBox containing numerous items.The code for populating the CheckedListBox is: 在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";
When i save data in the table, i am saving the 'ValueMember'.Now in Edit mode of the said form, i want CheckedListBox items to be Checked using the valuemember saved earlier.My problem is how to find the index of CheckedListBox items from its valuemember???Hope you understand my question. 当我将数据保存在表中时,我正在保存'ValueMember'.Now在上述表单的Edit模式下,我想使用之前保存的valuemember检查CheckedListBox项目。我的问题是如何从中找到CheckedListBox项目的索引它的有价值的成员?希望您理解我的问题。
while (rdrCCA.Read())
{
int index= clbCSA.Items.IndexOf(rdrCCA["CCA_ITEM_ID"]);
clbCSA.SetItemChecked(index, true);
}
where 哪里
clbCSA= name of the checkedlistbox control
CCA_ITEM_ID = name of the table field where valumember are being stored.
This code does not work.Please advice with some code. 此代码无效。请提供一些建议。
Since your data is in a dictionary, the most simple way to find the index by value is finding index of the value in dictionary this way: 由于您的数据在字典中,因此按值查找索引的最简单方法是通过以下方式在字典中查找值的索引:
var index = yourDictionary.Keys.ToList().IndexOf("SomeValue");
if(index > -1)
checkedListBox1.SetItemChecked(index, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.