none
When is ResetBindings required with DataGridView bound to BindlingList<T> of CLR objects? (WinForm C# .NET4) RRS feed

  • Question

  • I'm unclear when I need to invoke ResetBindings on a BindingList<T>  datasource that contains a list of CLR objects.

    In some cases, I seem to be able to update a property of one of the CLR objects in the BindingList, and the DataGridView gets updated automatically. But in other cases, it appears I need to invoke ResetBindings (which I would like to avoid doing whenever possible for performance reasons and to reduce grid "flashing").

    Also, if I programmatically set a new value in a cell of the DataGridView, should this new value be automatically propagated back to the property of the CLR object in the BindingList?

    Some of the articles I've read seem to give different answers to these questions. Or does the answer depend on the type of datasource the grid is bound to?

    Thanks,

    DT

     

    Thursday, February 3, 2011 5:37 PM

Answers

All replies

  • Hi pkellner,

     

    http://msdn.microsoft.com/en-us/library/f61k6akt

     

    Through this sample code, I think the ResetBindings need to be called when we modify the data member directly.

     

    And we also can see this statement:

    When you use data-bound controls, you sometimes have to respond to changes in the data source when the data source does not raise listchanged events.

     

    You can add a ListChanged event to your BindingSource to check if you can raise this event when you modify it, if this event can be raised, then you will not need to call the ResetBindings method, I think.

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 4, 2011 7:25 AM
    Moderator
  • I'll take a look at that article. I wonder if there's a summary somewhere that categorizes the various datasources as to whether they do (or don't) raise listchanged?

    But what about the inverse situation? That is, when I programmatically change a cell's value in a bound DataGridView, does that change get propagated back to the datasource? Seems like it should, since changes made by the user result in the datasource being updated.  But based on what I've seen, I'm not so sure. Particularly, if in an eventhandler (eg, CellValidating, etc) for cell "x" I change the value of cell "y", the change to "y" doesn't appear to be applied to the property of the CLR object in the BindiingList.

    Maybe what I really need is an article that describes the "standard Windows forms data binding model".   I've found that phrase in more than one MSDN article, but haven't found an article that explains the details of that "model" - particularly in regard to whether programmatic changes to the datasource or control are propagated to the other...

    Thanks,

    DT 

    Friday, February 4, 2011 4:06 PM
  • Hi dthouston,

     

    Windows Forms depends on a bound list to provide property change (a list item property value changes) and list changed (an item is deleted or added to the list) information to bound controls. Therefore, lists used for data binding must implement the IBindingList, which provides both types of change notification. TheBindingList<T> is a generic implementation of IBindingList and is designed for use with Windows Forms data binding. You can create a BindingList<T> that contains a business object type that implements INotifyPropertyChanged and the list will automatically convert the PropertyChanged events to ListChangedevents. If the bound list is not an IBindingList, you must bind the list of objects to Windows Forms controls by using the BindingSource component. TheBindingSource component will provide property-to-list conversion similar to that of the BindingList<T>. For more information, see How to: Raise Change Notifications Using a BindingSource and the INotifyPropertyChanged Interface.

    For details, you can read this document: Change Notification in Windows Forms Data Binding

     

    And the following articles and samples will be helpful for you to understand the notification in Windows Forms Data Binding:

    Implementing complex data binding in custom controls

     

    Monitoring the Data with Events

     

    A Detailed Data Binding Tutorial

     

    Data Binding in .NET / C# Windows Forms

     

    Data Binding Single-Value Controls at Run Time

     

    Data binding concepts in .NET windows forms

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information is found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Wednesday, February 9, 2011 2:28 AM
    Moderator
  • Hi dthouston,

     

    I am writing to check the status of the issue on your side.

     

    What about this problem now?

     

    Would you mind letting us know the result of the suggestions?

     

    If there's any concern, please feel free to let me know.

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 15, 2011 4:39 AM
    Moderator