Reputation: 51114
I have to resort to the viewmodel firing an event, and on catching that event, refreshing the whole grid. What is the point of something observable when it's not observed?
This is how may main form starts up, first populating the grid, and repopulating it every time something is added to the collection:
private void MainForm_Load(object sender, EventArgs e)
{
FoundFilesBindingSource.DataSource = ViewModel;
// TODO Try get rid of event model.
ViewModel.FilesFound += (o, args) =>
{
if (FileInfosGrid.InvokeRequired)
{
FileInfosGrid.Invoke(new Action(() => FileInfosGrid.DataSource = ViewModel.FileInfos));
}
else
{
FileInfosGrid.DataSource = ViewModel.FileInfos;
}
};
}
On class ViewModel
, FileInfos
is declared as:
public ObservableCollection<FindMatchViewModel> FileInfos { get; set; }
Upvotes: 0
Views: 493
Reputation: 3979
The ObservableCollection isn't working for a dataGridView because it doesn't implement IBindingList
. You have to use a BindingList
instead. Make sure that your items implement INotifyPropertyChanged
if you want to reflect changes to your properties.
The problem is that ObservableCollection is designed for Wpf Controls.
Notice that a BindingList
doesn't support sorting or filter your data and reflect this. This behaviour is only supported by DataTable or custom List implementations.
Upvotes: 3