如何从CheckedListBox获取选中项的值? [英] How to get value of checked item from CheckedListBox?

查看:1270
本文介绍了如何从CheckedListBox获取选中项的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中的WinForm上使用了CheckedListBox。我已将控件的边界如下所示-

I have used a CheckedListBox over my WinForm in C#. I have bounded this control as shown below -

chlCompanies.DataSource = dsCompanies.Tables[0];
chlCompanies.DisplayMember = "CompanyName";
chlCompanies.ValueMember = "ID";

我可以获取已检查项目的索引,但是如何获取已检查项目的文本和值。相反,我该如何枚举CheckedItems访问文本和值?

I can get the indices of checked items, but how can i get checked item text and value. Rather how can i enumerate through CheckedItems accessing Text and Value?

感谢您分享时间。

推荐答案

将其恢复为原始类型,如果要绑定表,它将为DataRowView,然后可以从相应的列中获取Id和Text:

Cast it back to its original type, which will be a DataRowView if you're binding a table, and you can then get the Id and Text from the appropriate columns:

foreach(object itemChecked in checkedListBox1.CheckedItems)
{
     DataRowView castedItem = itemChecked as DataRowView;
     string comapnyName = castedItem["CompanyName"];
     int? id = castedItem["ID"];
}

这篇关于如何从CheckedListBox获取选中项的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆