Datagridview, binding source, and invalid operation error

2.3k views Asked by At
 foreach (Listing s in fx)
            {
                ListingBindingSource.Add(s);
            }

This in my code causes the following error:

Invalid operation error: Operation is not valid due to the current state of the object.

Now i checked, I am running this on the main thread.

Also, it was working before. What changed recently is i changed Listing members, two of them, from int to string. I thought if i delete Listbindingsource and remake it, it would automatically change that. But it seems i have to do something else. Can anyone shed some light here. ListBindsource is the datasource to a datagridview. thanks

***SOLUTION******

 public Form1()
        {
            InitializeComponent();  Listing s1 = new  Listing();
             ListingBindingSource.Add(s1);
}

for some reason this added it. When the form was created for some reason null was being passed into the bindingsource. why, i don't know.

Some additional info:

If i remove the bindingsource as a datasource of the gridview, the error vanishes

1

There are 1 answers

4
indiPy On BEST ANSWER

"The Add method refers the call to the underlying list's Add method.

The internal list must contain homogenous types. If the DataSource property has not already been set, then the first object added to the list defines the type for the list"