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:
- 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;
}
}
}
- 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();
}
}
- 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
I just want to inherit the GridColumn,why I must inherit the GridView additionaly?
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?
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.