1

I have a CheckBoxList that displays around 20 options. I would like to iterate through and take the selected items from the list and insert them into 5 different columns in a database.

I disable the checkboxlist after 5 items are selected so I am good there. I just need to know how to iterate through and insert the selected items.

How would I get this accomplished? Thanks in advance!

0

3 Answers 3

2

you can get the checked checkboxes like this

var checkedCheckBoxes = this.Controls.OfType<CheckBox>()
                            .Where(c => c.Checked);

replace this with a container that has the checkboxes if it's not the outer most container such as a Form.

and then you can loop thru items in checkedCheckBoxes and formulate your insert statement.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you have list of checkboxes or CheckedListBox control?

As answers have been provided for first one already, take a look at CheckedIndices or CheckedItems properties of CheckedListBox control in case that's what you use.

Quick example:

// Cast<string>() should be replaced by whatever data type you use
var checkedItems = checkedListBox.CheckedItems.Cast<string>();
foreach (var item in checkedItems)
{
    Debug.WriteLine(item);
}

Comments

0

Try this:

CheckBoxList1.Items.Cast<CheckBox>()
                   .Where(s => s.Checked)
                   .Take(5);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.