The WinForms GridLookUpEdit
control does not support multiple item selection out of the box. This example demonstrates how to implement a "custom" Grid Lookup editor that allows users to select multiple values (rows) in its drop-down window.
The Grid Lookup editor is implemented based on a PopupContainerEdit control that displays a GridControl in the drop-down window.
The Grid Lookup editor's value is a string in CSV format. You can type comma-separated values (for example, "Name0, Name2, Name4") in the edit box to select corresponding rows in the dropdown. Users can also select rows in the dropdown. When the dropdown is closed, the Grid Lookup editor displays selected row values in the edit box.
WinForms LookUpEdit v.23.1+ Supports Multiple Item Selection
Read the following help topic for more information and examples: LookUpEdit - Enable Multiple Item Selection.
## Files to Review
- Customer.cs (VB: Customer.vb)
- Form1.cs (VB: Form1.vb)
- Program.cs (VB: Program.vb)
## Documentation
## See Also
## 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;
using System.Threading.Tasks;
namespace TestGridLookUpEditCSV {
public class Customer {
public Customer() {
this.ID = 0;
this.Name = "";
this.Info = "";
}
public Customer(int id, string name, string info) {
this.ID = id;
this.Name = name;
this.Info = info;
}
public int ID { get; set; }
public string Name { get; set; }
public string Info { get; set; }
}
}
C#using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestGridLookUpEditCSV {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
public Form1() {
InitializeComponent();
InitGrid();
RepositoryItemPopupContainerEdit ri = new RepositoryItemPopupContainerEdit();
ri.QueryResultValue += ri_QueryResultValue;
ri.EditValueChanged += ri_EditValueChanged;
ri.Popup += ri_Popup;
ri.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
PopupContainerControl popupControl = new PopupContainerControl();
popupControl.Controls.Add(popupGridControl1);
popupControl.Size = new System.Drawing.Size(500, 300);
ri.PopupControl = popupControl;
gridControl2.RepositoryItems.Add(ri);
gridView1.Columns["Name"].ColumnEdit = ri;
popupContainerEdit1.Properties.PopupControl = popupContainerControl1;
popupContainerEdit1.Properties.QueryResultValue += Properties_QueryResultValue;
popupContainerEdit1.Properties.EditValueChanged += Properties_EditValueChanged;
popupContainerEdit1.Properties.Popup += Properties_Popup;
}
void Properties_Popup(object sender, EventArgs e) {
PopupContainerEdit edit = sender as PopupContainerEdit;
if(!edit.IsPopupOpen)
edit.ShowPopup();
UpdateSelection(edit, popupGridView2);
}
void Properties_EditValueChanged(object sender, EventArgs e) {
PopupContainerEdit edit = sender as PopupContainerEdit;
UpdateSelection(edit, popupGridView2);
}
void Properties_QueryResultValue(object sender, DevExpress.XtraEditors.Controls.QueryResultValueEventArgs e) {
int[] selectedRows = popupGridView2.GetSelectedRows();
List<string> values = new List<string>();
foreach(int rowHandle in selectedRows) {
values.Add(popupGridView2.GetRowCellValue(rowHandle, "Name").ToString());
}
string csv = String.Join(", ", values);
e.Value = csv;
}
void ri_Popup(object sender, EventArgs e) {
PopupContainerEdit edit = sender as PopupContainerEdit;
UpdateSelection(edit, popupGridView1);
}
void ri_EditValueChanged(object sender, EventArgs e) {
PopupContainerEdit edit = sender as PopupContainerEdit;
if(!edit.IsPopupOpen)
edit.ShowPopup();
UpdateSelection(edit, popupGridView1);
}
void ri_QueryResultValue(object sender, DevExpress.XtraEditors.Controls.QueryResultValueEventArgs e) {
int[] selectedRows = popupGridView1.GetSelectedRows();
List<string> values = new List<string>();
foreach(int rowHandle in selectedRows) {
values.Add(popupGridView1.GetRowCellValue(rowHandle, "Name").ToString());
}
string csv = String.Join(", ", values);
e.Value = csv;
}
private void UpdateSelection(PopupContainerEdit edit, GridView view) {
view.BeginSelection();
view.ClearSelection();
if(edit != null)
if(edit.EditValue != null) {
edit.Focus();
List<int> selection = GetSelection(edit.EditValue.ToString().Split(new string[] { ", " }, StringSplitOptions.None), "Name", view);
foreach(int rowHandle in selection) {
view.SelectRow(rowHandle);
}
}
view.EndSelection();
}
private List<int> GetSelection(string[] values, string fieldName, GridView view) {
List<int> selection = new List<int>();
foreach(string val in values) {
for(int i = 0; i < view.RowCount; i++) {
if(view.GetRowCellValue(i, fieldName).ToString() == val)
selection.Add(i);
}
}
return selection;
}
private void InitGrid() {
popupGridControl1.DataSource = CreateList(5);
gridControl2.DataSource = CreateList(5);
popupGridControl2.DataSource = CreateList(5);
}
BindingList<Customer> CreateList(int count) {
BindingList<Customer> list = new BindingList<Customer>();
for(int i = 0; i < count; i++) {
list.Add(new Customer(i, "Name" + i, "Info" + i));
}
return list;
}
}
}
C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.UserSkins;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
namespace TestGridLookUpEditCSV
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
BonusSkins.Register();
SkinManager.EnableFormSkins();
UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");
Application.Run(new Form1());
}
}
}