A dictionary isn't a great candidate for binding--the Dictionary's
ValueCollection<T>
doesn't support IList, so it's not a very good match for a BindingSource. If you're stuck with a Dictionary then the always authoritative Marc Gravell has a solution here that may work for you.
If you don't have to use a dictionary then the typical, somewhat naïve approach is to set the BindingSource.DataSource
to a List<MyObject>
, and then set the DataGridView.DataSource
to that BindingSource. Once you have that set up, instead of adding/removing items through through the underlying List, you add/remove them through the BindingSource's add/remove methods--the UI will pick up those changes. If you change an existing item then you can call BindingSource.ResetItem
method to notify the UI that it needs to refresh that item.
But the best, most flexible approach would be to set your DataGridView.DataSource
property to a BindingList<MyObject>
, and, ideally, have your MyObject class implement INotifyPropertyChanged
... the BindingList notifies the UI of new items, and the INotifyPropertyChanged implementation notifies the UI about changes to existing items. This lets you modify the collection directly without having to worry about keeping the UI in sync.