windowsforms-116017-controls-and-libraries-editors-and-simple-controls-lookup-editors-using-a-dictionary-lookup-data-source.md
A lookup editor can be bound to a Dictionary<TKey,TValue>. The lookup processes keys as values and values as display text. Do not specify the ValueMember and DisplayMember properties.
Note
TKey and TValue should be of the String type or a value type (for example, keys are integers, and values are strings).
using System.Collections.Generic;
// Creates the dictionary.
Dictionary<int, string> products = new Dictionary<int, string>();
products.Add(0, "Product A");
products.Add(1, "Product B");
products.Add(2, "Product C");
products.Add(3, "Product D");
// Binds the lookup to the dictionary.
lookUpEdit1.Properties.DataSource = products;
Imports System.Collections.Generic
' Creates the dictionary.
Private products As New Dictionary(Of Integer, String)()
products.Add(0, "Product A")
products.Add(1, "Product B")
products.Add(2, "Product C")
products.Add(3, "Product D")
' Binds the lookup to the dictionary.
lookUpEdit1.Properties.DataSource = products
The animation below illustrates the result:
The lookup’s dropdown displays a column with display values. The lookup hides the column with values. The following code demonstrates how to display the column with values.
using System.Collections.Generic;
public Form1() {
InitializeComponent();
Dictionary<int, string> products = new Dictionary<int, string>();
products.Add(0, "Product A");
products.Add(1, "Product B");
products.Add(2, "Product C");
products.Add(3, "Product D");
lookUpEdit2.Properties.DataSource = products;
lookUpEdit2.Properties.ForceInitialize();
lookUpEdit2.Properties.PopulateColumns();
lookUpEdit2.Properties.Columns["Key"].Visible = true;
}
Imports System.Collections.Generic
Public Sub New()
InitializeComponent()
Dim products As New Dictionary(Of Integer, String)()
products.Add(0, "Product A")
products.Add(1, "Product B")
products.Add(2, "Product C")
products.Add(3, "Product D")
lookUpEdit2.Properties.DataSource = products
lookUpEdit2.Properties.ForceInitialize()
lookUpEdit2.Properties.PopulateColumns()
lookUpEdit2.Properties.Columns("Key").Visible = True
End Sub
In this example, the Data Grid embeds a lookup editor to display and edit values in the Category ID column. The lookup is bound to a dictionary with category IDs and names.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;
namespace LookUpEdit_Dictionary {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
// Binds the data grid to a data source.
gridControl1.DataSource = new List<Product> {
new Product(){ ProductName="Chang", CategoryID = 0 },
new Product(){ ProductName="Ipoh Coffee", CategoryID = 0 },
new Product(){ ProductName="Ravioli Angelo", CategoryID = 1 },
new Product(){ ProductName="Filo Mix", CategoryID = 1 },
new Product(){ ProductName="Tunnbröd", CategoryID = 1 },
new Product(){ ProductName="Konbu", CategoryID = 2 },
new Product(){ ProductName="Boston Crab Meat", CategoryID = 2 }
};
// Creates and initializes a dictionary with categories.
Dictionary<int, string> Categories = new Dictionary<int, string>();
Categories.Add(0, "Beverages");
Categories.Add(1, "Grains");
Categories.Add(2, "Seafood");
// Creates and initializes a lookup inplace editor (repository item).
RepositoryItemLookUpEdit riLookUp = new RepositoryItemLookUpEdit();
riLookUp.DataSource = Categories;
riLookUp.PopulateColumns();
riLookUp.Columns["Value"].Caption = "Name";
// Embeds the lookup editor into the data grid.
gridControl1.RepositoryItems.Add(riLookUp);
gridView1.Columns["CategoryID"].ColumnEdit = riLookUp;
}
}
public class Product {
public int CategoryID { get; set; }
public string ProductName { get; set; }
}
}
Imports DevExpress.XtraEditors.Repository
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Namespace LookUpEdit_Dictionary
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
gridControl1.DataSource = New List(Of Product) From { _
New Product() With {.ProductName="Chang", .CategoryID = 0}, _
New Product() With {.ProductName="Ipoh Coffee", .CategoryID = 0}, _
New Product() With {.ProductName="Ravioli Angelo", .CategoryID = 1}, _
New Product() With {.ProductName="Filo Mix", .CategoryID = 1}, _
New Product() With {.ProductName="Tunnbröd", .CategoryID = 1}, _
New Product() With {.ProductName="Konbu", .CategoryID = 2}, _
New Product() With {.ProductName="Boston Crab Meat", .CategoryID = 2} _
}
Dim Categories As New Dictionary(Of Integer, String)()
Categories.Add(0, "Beverages")
Categories.Add(1, "Grains")
Categories.Add(2, "Seafood")
Dim riLookUp As New RepositoryItemLookUpEdit()
riLookUp.DataSource = Categories
riLookUp.PopulateColumns()
riLookUp.Columns("Value").Caption = "Name"
gridControl1.RepositoryItems.Add(riLookUp)
gridView1.Columns("CategoryID").ColumnEdit = riLookUp
End Sub
End Class
Public Class Product
Public Property CategoryID() As Integer
Public Property ProductName() As String
End Class
End Namespace
See Also