0

I am using CheckedListBox control in windows forms. I need to bind that CheckedListBox Dynamically using entity model.

My Query:

private void BindTax()
        {
            try
            {
                eTax Tax = new eTax();
                cTax cTax= new cTax();
                List<eTax> ObjTax = cTax.GetTax(eGEntities);
                Tax.CategoryId = Convert.ToInt32("-1");
                Tax.CategoryName = "--Select--";
                ObjTax.Insert(0, Tax);
                foreach (eTax item in ObjTax)
                {
                    lstchkTax.Items.Add(item.TaxName);

                }                
           }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message.ToString());
            }

        }

I am getting Dispayname but I'm not getting the selectedValue. I am not finding the data source property for this control. Please tell me How can I do this?

Thanking you in advance.

RahulD
  • 709
  • 2
  • 16
  • 39
Sheela K R
  • 79
  • 1
  • 1
  • 8

1 Answers1

0

You have to cast it to a ListBox for the binding to work. I'm not sure why. The code would look like this...

((ListBox)lstchkTax).DataSource=ObjTax;
((ListBox)lstchkTax).DisplayMember="CategoryName";
((ListBox)lstchkTax).ValueMember="CategoryId";

A longer example can be found here.

Community
  • 1
  • 1
John Kraft
  • 6,811
  • 4
  • 37
  • 53