Example E1951
Visible to All Users

WinForms Data Grid - Implement a serializable property for the grid column

Important

This example uses internal APIs that may change in newer versions.

The VisibleIndex property returns -1 if a column is hidden. This example demonstrates how to implement a serializable property that stores the visible index of a column before the column is hidden. This allows you to save and restore the position of hidden columns when saving/loading the grid layout.

Implementation Details

  • Create a GridColumn descendant (MyGridColumn).
  • Implement a new property and apply the XtraSerializableAttribute attribute:
    C#
    private int oldVisibleIndex = -1; [XtraSerializableProperty, XtraSerializablePropertyId(LayoutIdLayout)] public int OldVisibleIndex { get { return oldVisibleIndex; } set { if (value == -1) return; oldVisibleIndex = value; } }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Q242361/Grid.cs(vb)
C#
using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Registrator; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Columns; using System.ComponentModel; using DevExpress.Utils.Serializing; namespace DXSample { public class MyGridControl : GridControl { public MyGridControl() : base() { } protected override void RegisterAvailableViewsCore(InfoCollection collection) { base.RegisterAvailableViewsCore(collection); collection.Add(new MyGridViewInfoRegistrator()); } } public class MyGridView : GridView { public MyGridView() : base() { } public MyGridView(GridControl grid) : base(grid) { } internal const string MyGridViewName = "MyGridView"; protected override string ViewName { get { return MyGridViewName; } } protected override GridColumnCollection CreateColumnCollection() { return new MyGridColumnCollection(this); } } public class MyGridViewInfoRegistrator : GridInfoRegistrator { public MyGridViewInfoRegistrator() : base() { } public override string ViewName { get { return MyGridView.MyGridViewName; } } public override BaseView CreateView(GridControl grid) { return new MyGridView(grid); } } public class MyGridColumnCollection : GridColumnCollection { public MyGridColumnCollection(ColumnView view) : base(view) { } protected override GridColumn CreateColumn() { return new MyGridColumn(); } } public class MyGridColumn : GridColumn { public MyGridColumn() : base() { } private int oldVisibleIndex = -1; [XtraSerializableProperty, XtraSerializablePropertyId(LayoutIdLayout)] public int OldVisibleIndex { get { return oldVisibleIndex; } set { if (value == -1) return; oldVisibleIndex = value; } } protected override void Assign(GridColumn column) { base.Assign(column); MyGridColumn source = column as MyGridColumn; if (source == null) return; OldVisibleIndex = source.OldVisibleIndex; } public override int VisibleIndex { get { return base.VisibleIndex; } set { oldVisibleIndex = value; base.VisibleIndex = value; } } public override bool Visible { get { return base.Visible; } set { if (!base.Visible && value && VisibleIndex == -1) VisibleIndex = OldVisibleIndex; base.Visible = value; } } } }

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.