Skip to main content

Bind Lookup to Dictionary

  • 4 minutes to read

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;

The animation below illustrates the result:

Bind to Dictionary - WinForms Lookup

Customize Lookup Columns

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.

Bind to Dictionary and Customize Columns - WinForms Lookup

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;
}

Example

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.

lookup-dictionary-example-result-animation.gif

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; }
    }


}
See Also