Current filter:
                                You should refresh the page.

                                How to make a Lookup editor function as a regular ComboBox

                                0

                                A Lookup editor is a kind of editors that can only display those values that are present in its dropdown list. Such editor can't display any value in its edit box. When a user types a value in the Lookup editor edit box, the ProcessNewValue fires. This event allows you to handle this situation and add the typed value to the editor's datasource. In this case, this value won't dissappear when the editor is validated.

                                However, if you need to have a fixed dropdown list and don't want to change the editor's datasource, this solution is not suitable for you. To overcome this problem, it's necessary to create a custom datasource wrapper. This wrapper will store the last typed value, and that's why the Lookup editor will show this value without any problem. However, this change won't affect a wrapped datasource.

                                This example is based on the How to create a data source wrapper that adds an empty item to the lookup list example. The only requirement is that your datasource should implement the ITypedList interface. You see that you can modify your original datasource values, and these changes will be automatically displayed in the GridLookUpEdit's datasource. However, changes made within the ProcessNewEvent event handler won't affect your original datasource.

                                • Eric Harmon_1 09.20.2012

                                  I successfully implemented this in my own project, with one small caveat that I've noticed so far. Let's say I have a data source with the values One, Two, and Three in it. My user types Four and saves the form.

                                  Then, the user opens the form again. My code says "GridLookUpEdit1.EditValue = "Four". But "Four" is not displayed in the GridLookUpEdit and the ProcessNewValue event doesn't fire. I realize I can manually look for "Four" in the datasource and if it's not there, I can set it before setting EditValue. However, I'm wondering if there's a way I can simply set the value in the GridLookUpEdit control and have the ProcessNewValue triggered, which I believe would take care of all this automatically for me.

                                You must  log in  or  register  to leave comments
                                Select file
                                • Form1.cs
                                • Program.cs
                                • MyDataSourceWrapper.cs
                                • MyGridLookupDataSourceHelper.cs
                                • MyObject.cs
                                Select language
                                • C#
                                • VB.NET
                                Select version
                                • v2010 vol 2.3 - v2012 vol 2.8
                                • v2009 vol 1.2 - v2010 vol 1.12
                                using System;
                                using System.Collections.Generic;
                                using System.ComponentModel;
                                using System.Data;
                                using System.Drawing;
                                using System.Text;
                                using System.Windows.Forms;
                                using DevExpress.XtraEditors;
                                
                                namespace WindowsApplication1
                                {
                                    public partial class Form1 : Form
                                    {
                                        private static DataTable CreateTable(int RowCount)
                                        {
                                            DataTable tbl = new DataTable();
                                            tbl.Columns.Add("Name", typeof(string));
                                            tbl.Columns.Add("ID", typeof(int));
                                            tbl.Columns.Add("Number", typeof(int));
                                            tbl.Columns.Add("Date", typeof(DateTime));
                                            for (int i = 0; i < RowCount; i++)
                                                tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, 3 - i, DateTime.Now.AddDays(i) });
                                            return tbl;
                                        }
                                
                                
                                        public Form1()
                                        {
                                            InitializeComponent();
                                            DataView dataSource = CreateTable(3).DefaultView;
                                            MyGridLookupDataSourceHelper.SetupGridLookUpEdit(gridLookUpEdit1, dataSource, "Name", "ID");
                                        }
                                    }
                                }