c# - Updating of BindingSource in WinForms does not update Datasource Collection

24
2014-04
  • Jeff Clare

    I want to display a custom collection in a DataGridView in a Windows Forms app. This custom collection implements ICollection, and IEnumerable. I have set up a BindingSource, using the collection as the .DataSource property. The DataGridView is set to use my BindingSource as it's DataSource. When I add a new item to the collection using the BindingSource.Add() method, the DataGridView updates correctly with the new item. The BindingSource DataSource, on the other hand, does not:

    MyCustomCollection myCollection = new MyCustomCollection();
    
    myCollection.Add(myCustomObject1);
    myCollection.Add(myCustomObject2);
    
    myBindingSource.DataSource(myCollection);
    myBindingSource.Add(myCustomObject3);
    

    In the above code, myBindingSource's internal List contains the right number of records (3), and the DataGridView also contains three records, but myCollection contains only two records. I know that changing the underlying myCollection will NOT update the BindingSource or the DataGridView, as it is not a BindingList<T>, but I was under the impression that updating a BindingSource directly would ensure that myCollection was updated at the same time.

    Is there a way to use a collection that isn't a BindingList<T> and have it updated when interacting with the BindingSource directly?

    Update: One way I've gotten the data updated across all the parts (Collection, BindingSource, DataGridView) is as follows:

    myCollection.Add(myCustomObject3);
    myBindingSource.DataSource = null;
    myBindingSource.DataSource = myCollection;
    

    I'm pretty sure there's a better way to attack this problem, but this is the only method that's generated the results I was expecting.

  • Answers
  • Eric J.

    You will have to manually call ResetBindings() after the data source changes if you use a container that cannot do that on your behalf.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.resetbindings.aspx

    Causes a control bound to the BindingSource to reread all the items in the list and refresh their displayed values.

  • Aaron Anodide

    I believe I ran into this issue a while ago - I did a find in files on my code and I think this is the solution that worked for me.

            // Applies pending changes to the underlying data source.
            this.bindingSource1.EndEdit();
    

    This was in the context of a click handler for the save button.


  • Related Question

    C# WinForms BindingList & DataGridView - disallowing EDIT prevents creation of a NEW row? How can I address this?
  • Greg

    Regarding my use of a DataGridView with BindingList, I was to disable editing current rows, but allow adding new rows. The issue I have is that when I disallows edits, this seems to prevent one from adding a new row item, as when you table into the cell for this new row it does not seem to allow editing???

    Know how to get around this? Section of my code below:

       BindingSource bs = new BindingSource();
       bList = new BindingList<Customer>();
       bList.AllowNew = true;
       bList.AllowEdit = false;
    
       // Fill bList with Customers
       bList.Add(new Customer("Ted"));
       bList.Add(new Customer("Greg"));
       bList.Add(new Customer("John"));
    
       bs.DataSource = bList;
       dataGridView1.DataSource = bs;
    

    thanks


  • Related Answers
  • Marc Gravell

    Rather than fight the source, perhaps ask the DataGridView to officiate:

    dataGridView1.DataSource = bs;
    dataGridView1.ReadOnly = true;
    dataGridView1.CurrentCellChanged += delegate 
    {
        DataGridViewRow row = dataGridView1.CurrentRow;
        bool readOnly = row == null ||
            row.Index != dataGridView1.NewRowIndex;
        dataGridView1.ReadOnly = readOnly;
    };
    

    (and don't set AllowEdit on the list)