Updating of BindingSource in WinForms does not update Datasource Collection

前端 未结 4 787
广开言路
广开言路 2021-02-14 00:57

I want to display a custom collection in a DataGridView in a Windows Forms app. This custom collection implements ICollection, and IEnumerable

4条回答
  •  既然无缘
    2021-02-14 01:10

    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);                
            }
    
        }
    

提交回复
热议问题