2

I have a Drop Down List . I need the value of my DDL when the index changes.So i do this :

 private void CMBGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
           int id=int.Parse(CMBGroup.SelectedValue.ToString());
           //do something with id
       }

In form load i fetch my data :

        goodGroups=objGoodGroupRepositoy.GetAll().ToList();
        CMBGroup.DataSource = goodGroups;
        CMBGroup.ValueMember = "Id";
        CMBGroup.DisplayMember = "Name";

I have such data in my database :

id serial    name
1   121    g1
2   123    g2

But i got this error before loading my form :

Input string was not in a correct format

I got this error in here in indexchange event of DDL

int id=int.Parse(CMBGroup.SelectedValue.ToString());

The model :

public partial class GoodGroup
    {
        public GoodGroup()
        {
            this.Goods = new HashSet<Good>();
        }

        public int Id { get; set; }
        public string Serial { get; set; }
        public string Name { get; set; }

        public virtual ICollection<Good> Goods { get; set; }
    }
10
  • Is it the same dropdown that gets triggered you are trying to read data from? Commented Jun 26, 2014 at 15:08
  • check SelectIndex before parsing : if (CMBGroup.SelectedIndex != -1 ){ ... Commented Jun 26, 2014 at 15:09
  • @LIUFA What do you mean ?sorry Commented Jun 26, 2014 at 15:09
  • 1
    Put a break point in this line, check what is the value of CMBGroup.SelectedValue? Also use TryParse to avoid the exception Commented Jun 26, 2014 at 15:10
  • 1
    set the DisplayMember and ValueMember properties before the DataSource Commented Jun 26, 2014 at 15:26

2 Answers 2

6

You should set the DisplayMember and ValueMember properties before the DataSource.

When the DataSource is changed, or when DisplayMember or ValueMember is changed after DataSource has been set, the binding infrastructure forces the control to rebind

Another way is to unsubscribe / subscribe to the event when modifying datasource.

Sign up to request clarification or add additional context in comments.

3 Comments

i used this code in another page and it worked why ?i mean i set the value and display property after datasource?
I've updated my answer. In your case hard to guess. Maybe you use a simple datasource and the toString() method is ok. Maybe you unsubscribe the SelectedIndexChanged event ?
Yes exactly iunsubscribe the SelectedIndexChanged event,by the way thank you
0

Instead of handling SelectedIndexChanged event, you can just handle the SelectionChangeCommitted event.

When you handle this event, you don't have to unsubscribe and resubscribe the event handler because it will only fire after you have changed the value of the combo box by selecting-not after you change the datasource.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.