Click here to Skip to main content
15,887,871 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I have a checkBoxList with items bound like this:

var accessories = from m_accessories in DB.Maintenance_Accessories
                  select m_accessories;

DataTable dt_Accessories = new DataTable();
dt_Accessories.Columns.Add("a_ID", typeof(int));
dt_Accessories.Columns.Add("a_Name", typeof(string));
foreach (var a in accessories)
{
    dt_Accessories.Rows.Add(a.Id, a.Name);
}
DataRow dr2 = dt_Accessories.NewRow();
dr2[0] = -1;
dr2[1] = "Other";
dt_Accessories.Rows.InsertAt(dr2, dt_Accessories.Rows.Count);
if (dt_Accessories.Rows.Count > 0)
{
    (chkList_Accessories as ListBox).DataSource = dt_Accessories;
    (chkList_Accessories as ListBox).DisplayMember = "a_Name";
    (chkList_Accessories as ListBox).ValueMember = "a_ID";
}


My problem now is that I want to Get the valueMember of the checked items in The checkBoxList, I tried this code but it didn't work:

Maintenance_Application_Accessory app_acc = new Maintenance_Application_Accessory();
for (int i = 0; i < chkList_Accessories.Items.Count - 1; i++)
{
    if (chkList_Accessories.GetItemCheckState(i) == CheckState.Checked)
    {
        //Here is my problem
        app_acc.Accessory_Id = int.Parse((chkList_Accessories as ListBox).ValueMember);
    }
}


What should I write else ?


Thanks in advance,

Best regards,
Michael Waguih
:)
Posted
Updated 21-Aug-13 0:05am
v2

1 solution

For every item which is checked, you'll need to treat it as a DataRow. In order to do so, you can just cast it as a DataRowView and look for the 'Column' which contains the value you want.
C#
int[] currentSelectedValue = new int[this.checkedListBox1.CheckedItems.Count];
for (int i = 0; i <= this.checkedListBox1.CheckedItems.Count - 1; i++)
{
    currentSelectedValue[i] = Convert.ToInt32((this.checkedListBox1.CheckedItems[i] as DataRowView)["ValueMember"]); ///The name of the column you use for the ValueMember property.
}

So in this case, look for the column which you've dedicated as the ValueMember column.
It's also easier to create a for loop which is only looping through checked items. :)

Hope this helps.
 
Share this answer
 
v3
Comments
Michael Waguih 21-Aug-13 8:21am    
It gives me an error that: "ValueMember is neither a DataColumn nor a DataRelation for table"

I used it like this:
app_acc.Accessory_Id = int.Parse((chkList_Accessories.CheckedItems[i] as DataRowView)["ValueMember"]);
Coder Jet 21-Aug-13 8:45am    
Where I put "ValueMember", you need to put the column name from the DataTable you linked it with. So looking at how you've hooked up the CheckListBox DataSource, you'll need to use "a_ID" instead, which is the column used for your ValueMember.

I shall amend my solution to make this more clear.
Michael Waguih 23-Aug-13 5:33am    
Thanks for your help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900