Updating of BindingSource in WinForms does not update Datasource Collection

梦想与她 提交于 2019-12-03 11:07:33
Frank

The problem is Fill Adaptor. When you load your form, the Fill is done for you. Just make sure to do a Refill and then follow up with Reset bindings post any data changes and Grid will get refreshed.

Example :

WorkTableAdapter.Insert(objData.XAttribute, "",
  objData.YAttribute,objLoanData.Amount_IsValid, DateTime.Now, DateTime.Now);
this.WorkTableAdapter.Fill(this.POCDataSet.Work);
this.WorkBindingSource.ResetBindings(false);

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.

Resetting an individual item works!

I had no luck with .ResetBindings(false) and re-assigning the datsource caused flickering with potentiail overhead if only one item change frequently.

I tried the built in mechanism using PropertyChanged but nothing updated.

Reseting an individual item using ResetItem() worked!

        for (int i = 0; i < bindingSource1.Count; i++)
        {
            bindingSource1.ResetItem(i);   
        }

And even better - if you have an update event attached to each data item in the bindningsource you can locate the object in the bindning source and use the index of the object to call ResetItem(idx)

In this case my custom event args contains a dictionary key to the data object contained in a separate collection. After object is located in using bindningsource.IndexOf() it is individually refreshed.

    void Value_PropertyChanged(object sender, RegisterEventArgs e)
    {

        var idx = bindingSource1.IndexOf(registers_ref[e.registerID]);
        if (idx>=0)
        {
            bindingSource1.ResetItem(idx);                
        }

    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!