Let's imagine a grid bound to a custom list of persons. The Person class has an Address property of the Address class type. You may want to display and edit subproperties of the Person.Address property in the grid. This example demonstrates how to accomplish this by implementing a custom property descriptor for Person.Address subproperties. Refer to How to display and edit complex data properties in grid columns to learn the implementation details of this example.
You must
log in
or
register
to leave comments
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TypeConverterTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private DevExpress.XtraGrid.GridControl gridControl1;
private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Persons persons = new Persons();
Address address = new Address("Obere Str. 57", "Berlin", "12209");
persons.Add(new Person("Maria Anderss", address));
address = new Address("120 Hanover Sq.", "London", "WA1 1DP");
persons.Add(new Person("Thomas Hardy", address));
address = new Address("12, rue des Bouchers", "Marseille", "13008");
persons.Add(new Person("Laurence Lebihan", address));
gridControl1.DataSource = persons;
gridControl1.MainView.PopulateColumns();
// implentent ConvertFrom, if you wish to edit Address
DevExpress.XtraGrid.Views.Grid.GridView gridView = gridControl1.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
DevExpress.XtraGrid.Columns.GridColumn c = gridView.Columns["Address"];
c.OptionsColumn.ReadOnly = true;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.gridControl1 = new DevExpress.XtraGrid.GridControl();
this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
this.SuspendLayout();
//
// gridControl1
//
this.gridControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.gridControl1.Location = new System.Drawing.Point(0, 0);
this.gridControl1.MainView = this.gridView1;
this.gridControl1.Name = "gridControl1";
this.gridControl1.Size = new System.Drawing.Size(671, 317);
this.gridControl1.TabIndex = 0;
this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
this.gridView1});
//
// gridView1
//
this.gridView1.GridControl = this.gridControl1;
this.gridView1.Name = "gridView1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(671, 317);
this.Controls.Add(this.gridControl1);
this.Name = "Form1";
this.Text = "How to display and edit complex data properties in grid columns";
((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
Facebook
Twitter
Google+