0

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; }
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • 1
    have you tried using a BindingList instead? – nozzleman Nov 05 '16 at 09:01
  • See http://stackoverflow.com/questions/4284663/difference-between-observablecollection-and-bindinglist – MSL Nov 05 '16 at 09:17
  • As addressed [here](https://stackoverflow.com/questions/33623991/connect-listt-to-a-listbox): • In Windows Forms, in a scenario that you want view changes of data source in the bound list control (complex two-way data binding), you should use a class that implements `IBindingList` as `DataSource` and the the most suitable implementation is `System.ComponentModel.BindingList`. • A common mistake is using `ObservableCollection` that will not work for this requirement since it doesn't implement `IBindingList` – Reza Aghaei Nov 05 '16 at 10:13
  • Also even if you use an implementation of `IBindingList`, it doesn't cause refreshing properties until you implement `INotifyPropertyChanged` for your class. – Reza Aghaei Nov 05 '16 at 10:16
  • 1
    *What is the point of something observable when it's not observed?* It depends to the requirement and the usage of the model. For example it would be useful in WPF. Also you can rely on `CollectionChanged` event to track changes on client and push changed entities to server for bulk CRUD operations like editing a list of items in a `DataGridView` control and the save them all in a single request to server. In general if your application is a Windows Forms Application without such requirement, I can't see any useful point in using `ObservableCollection` in this way. – Reza Aghaei Nov 05 '16 at 10:31

1 Answers1

3

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.

Sebi
  • 3,879
  • 2
  • 35
  • 62
  • There is some points in the answer which needs correction IMO: • `TreeView` doesn't support data-bindings so it can not bind to an `ObservableCollection`. •`AllowEdit` is `true` by default. – Reza Aghaei Nov 05 '16 at 10:51
  • @RezaAghaei oh thank you. Thought TreeView works because msdn is telling this: https://msdn.microsoft.com/de-de/library/ms668604(v=vs.110).aspx But they mean WPF Treeview i think. – Sebi Nov 05 '16 at 11:04
  • Yes the mean WPF [`TreeView`](https://msdn.microsoft.com/de-de/library/system.windows.controls.treeview(v=vs.110).aspx). – Reza Aghaei Nov 05 '16 at 11:09