Click here to Skip to main content
15,913,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to fill the items of the checkBoxList in windows Form with data using Linq.
and to give each item its MemberValue and DisplayMember like :

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 dr = dt_Accessories.NewRow();
dr[0] = -1;
dr2[1] = "Others";
dt_Accessories.Rows.InsertAt(dr, dt_Accessories.Rows.Count);

listBox_Accessories.DataSource = dt_Accessories;
listBox_Accessories.DisplayMember = "a_Name";
listBox_Accessories.ValueMember = "a_ID";


I faced a problem telling about DisplayMember and ValueMember:
This property is not relevant to this class
and in the same time it is not an error

Thanks in advance,


Best regards,
Michael Waguih
:)
Posted

1 solution

Strange how it is not error'in, since I'm sure there shouldn't be a .DataSource property for a CheckListBox.

In order for you to set the DataSource, DisplayMember and ValueMember for the CheckListbox is to cast it as a ListBox, like so:

C#
DataTable temp = new DataTable();
temp.Columns.Add("ValueMember", typeof(byte));
temp.Columns.Add("DisplayMember", typeof(string));
temp.Rows.Add(0, "Zero");
temp.Rows.Add(1, "One");
temp.Rows.Add(2, "Two");
temp.Rows.Add(3, "Three");
temp.Rows.Add(4, "Four");
temp.Rows.Add(5, "Five");
 
(this.checkedListBox1 as ListBox).DataSource = temp;
(this.checkedListBox1 as ListBox).DisplayMember = "DisplayMember";
(this.checkedListBox1 as ListBox).ValueMember = "ValueMember";


Hope this Helps.
 
Share this answer
 
Comments
Michael Waguih 21-Aug-13 3:04am    
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