How to loop through a checkboxlist and to find what's checked and not checked?
Asked Answered
V

6

21

I'm trying to loop through items of a checkbox list. If it's checked, I want to set a value. If not, I want to set another value. I was using the below, but it only gives me checked items:

foreach (DataRowView myRow in clbIncludes.CheckedItems)
{
    MarkVehicle(myRow);
}
Vav answered 27/12, 2008 at 21:46 Comment(1)
asp316: WebForms or WinForms? (The APIs surrounding their respective CheckBoxList controls are quite different.)Audient
H
28
for (int i = 0; i < clbIncludes.Items.Count; i++)
  if (clbIncludes.GetItemChecked(i))
    // Do selected stuff
  else
    // Do unselected stuff

If the the check is in indeterminate state, this will still return true. You may want to replace

if (clbIncludes.GetItemChecked(i))

with

if (clbIncludes.GetItemCheckState(i) == CheckState.Checked)

if you want to only include actually checked items.

Henpeck answered 28/12, 2008 at 0:13 Comment(2)
Using this worked great. How can I get the value/value member of the checked checkbox?Vav
clbIncludes.GetItemChecked(i) was not a valid method for me, but I simply replaced it with clbIncludes.Items[i].SelectedWhimsy
S
32

This will give a list of selected

List<ListItem> items =  checkboxlist.Items.Cast<ListItem>().Where(n => n.Selected).ToList();

This will give a list of the selected boxes' values (change Value for Text if that is wanted):

var values =  checkboxlist.Items.Cast<ListItem>().Where(n => n.Selected).Select(n => n.Value ).ToList()
Sutherland answered 27/9, 2010 at 12:53 Comment(1)
I get error for both of them.. with first solution i get error 'Cannot implicitly convert type 'System.Collections.Generic.List<System.Web.UI.WebControls.ListItem>' to 'System.Web.UI.WebControls.ListItem'' and with second i get error at run time The name 'values' does not exist in the current contextMacy
H
28
for (int i = 0; i < clbIncludes.Items.Count; i++)
  if (clbIncludes.GetItemChecked(i))
    // Do selected stuff
  else
    // Do unselected stuff

If the the check is in indeterminate state, this will still return true. You may want to replace

if (clbIncludes.GetItemChecked(i))

with

if (clbIncludes.GetItemCheckState(i) == CheckState.Checked)

if you want to only include actually checked items.

Henpeck answered 28/12, 2008 at 0:13 Comment(2)
Using this worked great. How can I get the value/value member of the checked checkbox?Vav
clbIncludes.GetItemChecked(i) was not a valid method for me, but I simply replaced it with clbIncludes.Items[i].SelectedWhimsy
C
23

Try something like this:

foreach (ListItem listItem in clbIncludes.Items)
{
    if (listItem.Selected) { 
        //do some work 
    }
    else { 
        //do something else 
    }
}
Conversion answered 27/12, 2008 at 21:58 Comment(2)
It's winform. So, when i try to reference listitem, it's wanting to reference a the web control. i tried using a listviewitem and get the error 'Unable to cast object of type 'System.Data.DataRowView' to type 'System.Windows.Forms.ListViewItem'. ThoughtsVav
bill martin if it's winform then remove your tag that says .net. I spent some time trying to figure out why your solution didn't work for me (using .net) when I realized the GetItemChecked method only applies to winforms.Countenance
B
2

I think the best way to do this is to use CheckedItems:

 foreach (DataRowView objDataRowView in CheckBoxList.CheckedItems)
 {
     // use objDataRowView as you wish                
 }
Bloomsbury answered 9/6, 2013 at 13:27 Comment(1)
I didn't find a CheckedItems properties for CheckBoxList.Tiro
Q
1

check it useing loop for each index in comboxlist.Items[i]

bool CheckedOrUnchecked= comboxlist.CheckedItems.Contains(comboxlist.Items[0]);

I think it solve your purpose

Qianaqibla answered 30/6, 2012 at 7:10 Comment(0)
E
0

Use the CheckBoxList's GetItemChecked or GetItemCheckState method to find out whether an item is checked or not by its index.

Enfilade answered 27/12, 2008 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.