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
- 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.
- Override the
ViewName
property for theGridView
descendant. This property should return a unique View name. - Register the
GridView
descendant for design-time use:- Create a
GridInfoRegistrator
class descendant. ItsViewName
property must return the same value as your View'sViewName
property. - Override the
RegisterAvailableViewsCore
method in yourGridControl
descendant. - If you want the custom
GridControl
to create an instance of theGridView
descendant on the grid's initialization, override theGridControl.CreateDefaultView
method. - Mark your grid control with the
[ToolboxItem(true)]
attribute.
- Create a
- Rebuild the solution to display the
MyGridControl
in the Visual Studio Toolbox. - Locate MyGridControl` in the Visual Studio Toolbox, drag and drop it on a Form.
Files to Review
- Customer.cs (VB: Customer.vb)
- Form1.cs (VB: Form1.vb)
- MyGridControl.cs (VB: MyGridControl.vb)
- MyGridHandler.cs (VB: MyGridHandler.vb)
- MyGridRegistration.cs (VB: MyGridRegistration.vb)
- MyGridView.cs (VB: MyGridViewInfo.vb)
- MyGridViewInfo.cs (VB: MyGridViewInfo.vb)
- Program.cs (VB: Program.vb)
See Also
- Create GridView descendants
- DevExpress WinForms Troubleshooting - Grid Control
- How to create a WinExplorerView descendant class and register it for design-time use
- How to create a LayoutView descendant class and register it for design-time use
- How to create a CardView descendant class and register it for design-time use
- How to create an AdvBandedGridView descendant class and register it for design-time use
- How to create a BandedGridView descendant class and register it for design-time use
- How to create a TileView descendant class and register it for design-time use
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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; }
}
}
}
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;
}
}
}
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());
}
}
}
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);
}
}
}
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); }
}
}
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"; } }
}
}
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;
}
}
}
}
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());
}
}
}