0

so, I have a checkListBox that i'm trying to get the value member of the checked boxes. Currently, I can get the selectedValue of one item. If multiple items are checked, i get the same selectedValue for each.

The box is populated like so...

SqlConnection cn = new SqlConnection(Properties.Settings.Default.cs);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("usp_getCustomers, cn);
DataSet ds = new DataSet();
da.Fill(ds, "usp_getCustomers");
chkListCustomer.DataSource = ds;
chkListCustomer.DisplayMember = "usp_getCustomers.name";
chkListCustomer.ValueMember = "usp_getCustomers.id";
chkListCustomer.SelectedIndex = -1;

On a button click, this is what i'm doing to TRY and get the selected values. It gives me the right ID for one item, but if multiple items are checked, it gives the same ID back for all.

foreach (int indexChecked in chkListCustomer.CheckedIndices)
{
    MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + chkListCustomer.SelectedValue.ToString()  + ".");   
}

example output is:

"Index#: 1, is checked. Checked state is:984"  
"Index#: 2, is checked. Checked state is:984"  
"Index#: 3, is checked. Checked state is:984" 

thanks for the help!

VMAtm
  • 27,943
  • 17
  • 79
  • 125
dave k
  • 1,329
  • 4
  • 22
  • 44
  • 1
    found my answer from a similar post answered by Ahmad Mageed at http://stackoverflow.com/questions/4875540/how-to-get-value-of-checked-item-from-checkedlistbox will update it as the answer later when SO will allow me to...my rep is under 100, can't self answer for a while. :( – dave k Jul 19 '11 at 19:47

4 Answers4

1

Try this:

foreach (int indexChecked in chkListCustomer.CheckedIndices)
{
    MessageBox.Show("Index#: " + indexChecked.ToString() +
        ", is checked. Checked state is:" +
        chkListCustomer.Items[indexChecked].ToString()  + ".");   
}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • this returns "System.Data.DataRowView" instead of a number – dave k Jul 19 '11 at 19:04
  • @dave k: Then you need to cast `chkListCustomer.Items[indexChecked]` to `DataRowView` and inspect that object. I can't tell just from the code how the value object is created, since it's done with binding. But that `DataRowView` object contains the value you are looking for somewhere. You just need to figure out how to extract it. – cdhowie Jul 19 '11 at 19:06
1

If you don't actually need the index, you could simply use the CheckedItems property:

foreach (DataRowView checkedItem in chkListCustomer.CheckedItems)
{
    MessageBox.Show("Checked item: "
        + checkedItem[chkListCustomer.ValueMember].ToString()
        + ".");
}
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • this returns "System.Data.DataRowView" instead of a number...and you're right, i don't need the index, i was just stealing the sample code off MSDN – dave k Jul 19 '11 at 19:07
  • @dave k: Aha, then you need to index that view to get the data you are interested in. Let me edit the answer. – Merlyn Morgan-Graham Jul 19 '11 at 19:09
  • i get an error, usp_getCustomers.id is neither a DataColumn nor a DataRelation for table usp_getCustomers – dave k Jul 19 '11 at 19:19
  • Seems you're stuck at the same place as cdhowie's answer. I haven't used this control, so I don't really know how to help you further :) Maybe try `int` indices (such as 0), `DisplayMember`, or column names from your table. – Merlyn Morgan-Graham Jul 19 '11 at 19:25
0

You should use

chkListCustomer.GetItemCheckState(indexChecked).ToString()

instead of

chkListCustomer.SelectedValue.ToString()

More info on MSDN about CheckedIndices and CheckedListBox Class.

Also you can iterate through the .Items property:

foreach(object itemChecked in chkListCustomer.CheckedItems) {
    // Use the IndexOf method to get the index of an item.
    MessageBox.Show("Item with title: \"" + itemChecked.ToString() + 
                    "\", is checked. Checked state is: " + 
        chkListCustomer.GetItemCheckState(chkListCustomer.Items.IndexOf(itemChecked)).ToString() + ".");
    MessageBox.Show(itemChecked.ToString())
}
VMAtm
  • 27,943
  • 17
  • 79
  • 125
0

modified a piece of code found on SO on this post from Ahmad Mageed. This gives me each

    foreach (object itemChecked in chkListPatients.CheckedItems) 
    {
        DataRow row = (itemChecked as DataRowView).Row;
        string id = row[0].ToString();
        MessageBox.Show(id);
    }
Community
  • 1
  • 1
dave k
  • 1,329
  • 4
  • 22
  • 44