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";
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?
Thanks for sharing your time.
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:
EDIT: I realized a little late that it was bound to a DataTable. In that case the idea is the same, and you can cast to a
DataRowView
then take itsRow
property to get aDataRow
if you want to work with that class.You would need to cast or parse the items to their strongly typed equivalents, or use the
System.Data.DataSetExtensions
namespace to use theDataRowExtensions.Field
method demonstrated below:You need to cast the item to access the properties of your class.
Alternately, you could use the
OfType
extension method to get strongly typed results back without explicitly casting within the loop:To get the all selected Items in a CheckedListBox try this:
In this case ths value is a String but it's run with other type of Object:
Egypt Development Blog : Get value of checked item in CheckedListBox in vb.net
after bind CheckedListBox with data you can get value of checked items
now you can use the value or Display of checked items in CheckedListBox from the 2 variable XDisplayMember And XValueMember in the loop
hope to be useful.
You can iterate over the
CheckedItems
property:http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditems.aspx