Skip to main content

How to: Create and customize a custom GridLookUpEdit control

  • 4 minutes to read

Assume that a GridLookUpEdit control needs to be used in multiple places, and all editors must display data from one predefined data source and contain predefined columns. In this instance, you can create a custom GridLookUpEdit control.

This example shows how to create a custom GridLookUpEdit control, and initialize its properties as required. The structure of descendant classes is based on the code template shown in the Custom Editors.Editors Structure topic.

To customize the control’s settings, the OnLoaded method is overridden. In this method, the control’s DataSource, columns and some other options are initialized. Note that columns created in this method are not accessible at design time.

using System.Drawing;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System.Data;


namespace DevExpress.MyGridLookUpEditors {

    //The attribute that points to the registration method
    [UserRepositoryItem("RegisterMyGridLookUpEdit")]
    public class RepositoryItemMyGridLookUpEdit : RepositoryItemGridLookUpEdit {

        //The static constructor which calls the registration method
        static RepositoryItemMyGridLookUpEdit() { RegisterMyGridLookUpEdit(); }

        //Initialize new properties
        public RepositoryItemMyGridLookUpEdit() {
        }

        protected override void OnLoaded() {
            base.OnLoaded();
            if (IsDesignMode) return;
            // Create two columns
            GridColumn colId = new GridColumn();
            colId.FieldName = colId.Caption = "ID";
            colId.VisibleIndex = 0;
            GridColumn colName = new GridColumn();
            colName.FieldName = colName.Caption = "Name";
            colName.VisibleIndex = 1;
            GridView gView = this.View;
            gView.Columns.Clear();
            gView.Columns.Add(colId);
            gView.Columns.Add(colName);
            // Hide the group panel
            gView.OptionsView.ShowGroupPanel = true;
            // Initialize data source
            DisplayMember = "Name";
            ValueMember = "ID";
            DataSource = GetData();
        }

        private DataTable GetData() {
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("ID", typeof(int)));
            table.Columns.Add(new DataColumn("Name", typeof(string)));
            table.Rows.Add(new object[] { 0, "A" });
            table.Rows.Add(new object[] { 1, "B" });
            table.Rows.Add(new object[] { 2, "C" });
            return table;
        }


        //The unique name for the custom editor
        public const string MyGridLookUpEditName = "MyGridLookUpEdit";

        //Return the unique name
        public override string EditorTypeName { get { return MyGridLookUpEditName; } }

        //Register the editor
        public static void RegisterMyGridLookUpEdit() {
            //Icon representing the editor within a container editor's Designer
            Image img = null;

            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(MyGridLookUpEditName,
              typeof(MyGridLookUpEdit), typeof(RepositoryItemMyGridLookUpEdit),
              typeof(GridLookUpEditBaseViewInfo), new ButtonEditPainter(), true, img));
        }



        //Override the Assign method
        public override void Assign(RepositoryItem item) {
            BeginUpdate();
            try {
                base.Assign(item);
                RepositoryItemMyGridLookUpEdit source = 
                    item as RepositoryItemMyGridLookUpEdit;
                if (source == null) return;
            }
            finally {
                EndUpdate();
            }
        }
    }


    public class MyGridLookUpEdit : GridLookUpEdit {

        //The static constructor which calls the registration method
        static MyGridLookUpEdit() { 
            RepositoryItemMyGridLookUpEdit.RegisterMyGridLookUpEdit(); }

        //Initialize the new instance
        public MyGridLookUpEdit() {
            //...
        }

        //Return the unique name
        public override string EditorTypeName { get { return 
            RepositoryItemMyGridLookUpEdit.MyGridLookUpEditName; } }

        //Override the Properties property
        //Simply type-cast the object to the custom repository item type
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemMyGridLookUpEdit Properties {
            get { return base.Properties as RepositoryItemMyGridLookUpEdit; }
        }

    }
}