Example E900
Visible to All Users

WinForms Data Grid - How to create a GridView descendant and use it at design time

In v16.1+, you can use the Custom Data Grid template shipped as part of the DevExpress Template Gallery to create a GridView descendant with pre-built infrastructure classes.

This example demonstrates how to create a custom view (a GridView descendant) from scratch. The descendant View allows users to delete records by pressing the Delete key. Open the solution and build the project before opening Form1 in the Visual Studio designer.

How it Works - Descendant View Concept

  1. Create descendants of the following classes and implement custom functionality:
    • GridView - The component that exposes customization settings and references all infrastructure classes.
    • GridControl - The UI control that you place on the form. This is a container for the View.
    • GridInfoRegistrator - Contains the View's registration information.
    • GridHandler - Processes mouse and keyboard actions.
    • GridViewInfo - Calculates display information used to draw View elements.
    • GridViewPrintInfo - Calculates the information required to print the View.
  2. Override the ViewName property for the GridView descendant. This property should return a unique View name.
  3. Register the GridView descendant for design-time use:
    • Create a GridInfoRegistrator class descendant. Its ViewName property must return the same value as your View's ViewName property.
    • Override the RegisterAvailableViewsCore method in your GridControl descendant.
    • If you want the custom GridControl to create an instance of the GridView descendant on the grid's initialization, override the GridControl.CreateDefaultView method.
    • Mark your grid control with the [ToolboxItem(true)] attribute.
  4. Rebuild the solution to display the MyGridControl in the Visual Studio Toolbox.
  5. Locate MyGridControl` in the Visual Studio Toolbox, drag and drop it on a Form.

Files to Review

See Also

Does this example address your development requirements/objectives?

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

Example Code

MyXtraGrid/Customer.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyXtraGrid { public class Customer { string name; int age; int weight; int height; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public int Weight { get { return weight; } set { weight = value; } } public int Height { get { return height; } set { height = value; } } } }
MyXtraGrid/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MyXtraGrid { public partial class Form1 : Form { BindingList<Customer> customers; public Form1() { InitializeComponent(); FillGridDataSource(); } private void FillGridDataSource() { customers = new BindingList<Customer>(); for (int i = 0; i < 50; i++) { Customer customer = new Customer(); customer.Name = "Mike"; if (i % 2 == 0) customer.Name = "John"; customer.Age = 70 - i; customer.Weight = 50 + i; customer.Height = 150 + i; customers.Add(customer); } myGridControl1.DataSource = customers; } } }
MyXtraGrid/MyGridControl.cs(vb)
C#
using System; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Registrator; namespace MyXtraGrid { public class MyGridControl : GridControl { protected override BaseView CreateDefaultView() { return CreateView("MyGridView"); } protected override void RegisterAvailableViewsCore(InfoCollection collection) { base.RegisterAvailableViewsCore(collection); collection.Add(new MyGridViewInfoRegistrator()); } } }
MyXtraGrid/MyGridHandler.cs(vb)
C#
using System; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; namespace MyXtraGrid { public class MyGridHandler : DevExpress.XtraGrid.Views.Grid.Handler.GridHandler { public MyGridHandler(GridView gridView) : base(gridView) {} protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if(e.KeyData == Keys.Delete && View.State == GridState.Normal) View.DeleteRow(View.FocusedRowHandle); } } }
MyXtraGrid/MyGridRegistration.cs(vb)
C#
using System; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Base.Handler; using DevExpress.XtraGrid.Views.Base.ViewInfo; using DevExpress.XtraGrid.Registrator; namespace MyXtraGrid { public class MyGridViewInfoRegistrator : GridInfoRegistrator { public override string ViewName { get { return "MyGridView"; } } public override BaseView CreateView(GridControl grid) { return new MyGridView(grid as GridControl); } public override BaseViewInfo CreateViewInfo(BaseView view) { return new MyGridViewInfo(view as MyGridView); } public override BaseViewHandler CreateHandler(BaseView view) { return new MyGridHandler(view as MyGridView); } } }
MyXtraGrid/MyGridView.cs
C#
using System; namespace MyXtraGrid { public class MyGridView : DevExpress.XtraGrid.Views.Grid.GridView { public MyGridView() : this(null) {} public MyGridView(DevExpress.XtraGrid.GridControl grid) : base(grid) { // put your initialization code here } protected override string ViewName { get { return "MyGridView"; } } } }
MyXtraGrid/MyGridViewInfo.cs(vb)
C#
using System; using System.Drawing; using DevExpress.XtraGrid.Views.Grid.ViewInfo; namespace MyXtraGrid { public class MyGridViewInfo : GridViewInfo { public MyGridViewInfo(DevExpress.XtraGrid.Views.Grid.GridView gridView) : base(gridView) {} public override int CalcRowHeight(Graphics graphics, int rowHandle, int rowVisibleIndex, int min, int level, bool useCache, GridColumnsInfo columns) { return base.CalcRowHeight(graphics, rowHandle, rowVisibleIndex, MinRowHeight, level, useCache, columns); } public override int MinRowHeight { get { return base.MinRowHeight - 2; } } } }
MyXtraGrid/Program.cs(vb)
C#
using System; using System.Collections.Generic; using System.Windows.Forms; namespace MyXtraGrid { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

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.