Description:
I have a grid bound to a collection of custom objects. I've implemented the IBindingList interface in my collection class. But I started to get a serialization exception saying that the XtraGrid is not serializable. Any suggestions?
Answer:
The cause of the error is that the standard serializer tries to save all linked objects together with the object being serialized. For example, you wish to save your collection of data. The collection implements the IBindingList interface and therefore it's linked with the grid via the ListChanged event. The solution is to mark the listChangedHandler fields in your collection class with the NonSerialized attribute. Below is the necessary C# code.
C#[NonSerialized()]
private ListChangedEventHandler listChangedHandler;
Note This solution cannot be used in VB.NET, because it has a different event structure than C#. It makes it impossible to apply the NonSerialized attribute to an event. The serialization error has nothing to do with the XtraGrid implementation. You can reproduce it with a standard grid as well.