KB Article A927
Visible to All Users

How to inherit grid columns and use them in the XtraGrid

Description:
I need to create a new class derived from the GridColumn class to add some properties and implement specific functionality widely used in my application. Is it possible?

Answer:
Yes, it is possible. Here is the list of classes to inherit from and the methods which should be overridden to make a custom column available at design time:

  1. Create a descendant from the DevExpress.XtraGrid.Columns.GridColumn class (see GridColumns.cs). Implement the functionality you need. If you add more properties to your columns and want them to be saved via the SaveLayoutToXml method, your should use the XtraSerializableProperty attribute.
C#
public class MyGridColumn : GridColumn { public MyGridColumn() { } string customDataValue = string.Empty; [XtraSerializableProperty()] public string CustomData { get { return customDataValue; } set { customDataValue = value; } } protected override void Assign(GridColumn column) { base.Assign(column); if(column is MyGridColumn) { this.CustomData = ((MyGridColumn)column).CustomData; } } }
  1. Create a descendant from the DevExpress.XtraGrid.Columns.GridColumnCollection class (GridColumns.cs). Override the CreateColumn method in it to create an instance of your column class instead of our GridColumn.
C#
using DevExpress.XtraGrid.Views.Base; public class MyGridColumnCollection : GridColumnCollection { public MyGridColumnCollection(ColumnView view) : base(view) {} protected override GridColumn CreateColumn() { return new MyGridColumn(); } }
  1. Create a descendant from the DevExpress.XtraGrid.Views.Grid.GridView class (Standard\GridView.cs). Override the CreateColumnCollection method to create an instance of your column collection.
C#
public class MyGridView : DevExpress.XtraGrid.Views.Grid.GridView { public MyGridView() : this(null) {} public MyGridView(DevExpress.XtraGrid.GridControl grid) : base(grid) {} protected override GridColumnCollection CreateColumnCollection() { return new MyGridColumnCollection(this); } }

See Also:
How to create a GridView descendant class and register it for design-time use
How to add a quick columns customization drop-down menu to a GridView

Show previous comments (1)
DevExpress Support Team 13 years ago

    GridView can create columns automatically to display fields from the underlying datasource or when a layout is being restored. If you want to use these features as well as design-time features, you will need to create a GridView descendant. Note that you can create a GridColumn descendant manually and add it to the column collection. In this case, this column will function as a standard column.

      Hi, I'm using this example but, I don't Save and Restore Layout ColumnEdit in RepositoryItem.
      How to Save and Restore Layout ColumnEdit in RepositoryItem?

      DevExpress Support Team 11 years ago

        Hello Ahmet,
        To avoid discussing multiple topics in this thread, I have extracted your original inquiry to a separate ticket created on your behalf: T102741: How to Save and Restore Layout ColumnEdit in RepositoryItem.

        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.