I have a CheckBoxList which I'm populating with data. When I attempt to retrieve the checked items from the list I can only grab the item ordinal, I cannot get the value.
I've read that you can use Items[i].Value however when I try to do this I get an error stating that there is no extension method 'value'.
Here's the code I'm using to try and grab the information (note the GetItemText(i) actually only gives me the item position, not the text for the item)
private void btnGO_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.GetItemChecked(i))
{
string str = chBoxListTables.GetItemText(i);
MessageBox.Show(str);
//next line invalid extension method
chBoxListTables.Items[i].value;
}
}
}
This is using .Net 4.0
Any thoughts would be appreciated...thanks
You can try this:-
//Simple example code:
This ended up being quite simple. chBoxListTables.Item[i] is a string value, and an explicit convert allowed it to be loaded into a variable. The following code works:
Instead of this:
Try This:
It worked for me :)
You can initialize a list of string and add those items that are selected.
Please check code, works fine for me.
to get the items checked you can use
CheckedItems
orGetItemsChecked
. I tried below code in .NET 4.5Iterate through the
CheckedItems
collection. This will give you the item number in the list of checked items, not the overall list. So if the first item in the list is not checked and the second item is checked, the code below will display text likeChecked Item 1 = MyListItem2
.-OR-
Step through the Items collection and call the
GetItemChecked
method for each item. This will give you the item number in the overall list, so if the first item in the list is not checked and the second item is checked, it will display something likeItem 2 = MyListItem2
.Hope this helps...