KB Article A2742
Visible to All Users

What could be the cause of a serialization error when I save my custom collection which is bound to a grid?

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.

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.