Weird DataGridView Behavior

I can't understand why this happens. I have a page level variable that stores an object I call EventCollection. That object contains a List<> of things. I use the code below to bind the list to a DataGridView.
this.dataGridViewEvents.DataSource = null;
if (this.EventCollection.List.Count > 0)
    // Don't assign an empty List<> to a DataGridView DataSource. It causes weirdness when selecting rows if they're added later.
    this.dataGridViewEvents.DataSource = this.EventCollection.List;

Open in new window

Later on if I want to add a new item I simply do this:
SierraEvent newEvent = new SierraEvent(this.ParentId);

Open in new window

Then I rebind the datasource to the DataGridView control. This works fine as long as I make sure not to assign an empty list as the datasource. If I do that then I get a very odd behavior. After adding items to an empty list, if I then select a row in the DataGridView I get the following Exception:
System.IndexOutOfRangeException occurred
  _message=Index -1 does not have a value.
  Message=Index -1 does not have a value.
       at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)

Open in new window

I've managed to avoid the problem using the above logic but I am at a loss to understand why this happens in the first place. Is this a bug in the DataGridView control?
LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
Just to confirm  -- after this:

this.dataGridViewEvents.DataSource = this.EventCollection.List;

Open in new window

you then actually do the bind, right?


Open in new window

Also, on the grid view control, do you have AppendDataBoundItems set to true or false?
Russ SuterSenior Software DeveloperAuthor Commented:
There is no DataBind() method in the DataGridView control. Nor is there an AppendDataBoundItems property.
You could also look into using a List/Binding pair that already implements the concurrency logic; e.g. - BindingList with BindingSource.

It just depends on what the implementation of EventCollection is.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.