Skip to main content
A newer version of this page is available. .

Using a Dictionary Lookup Data Source

  • 4 minutes to read

The DevExpress Windows Forms lookup editors support easy initialization when their lookup data sources are Dictionary<TKey, TValue> objects, provided that TKey and TValue are simple data types (e.g., integer, string, etc.). This topic shows how to set up a lookup editor in this case.

lookup-dictionary-animation.gif

Assume that you have a field of a simple data type (e.g., integer). This field values should be presented using associated display values. For instance, instead of category IDs you should display category names. One of solutions to this task is using a lookup editor whose lookup data source is a Dictionary<TKey, TValue> object.

Create a Dictionary<TKey, TValue> object that associates field values (of the TKey type) with display values (of the TValue type), and use this Dictionary as a lookup data source (assign it to the RepositoryItemLookUpEditBase.DataSource property).

For a Dictionary<TKey, TValue> lookup data source, the lookup editor automatically creates two columns (“Key” and “Value”) in the dropdown. The “Key” column values match the lookup editor’s bound field values. Thus, when an end-user selects a row in the dropdown, this row’s Key column value is assigned to the lookup editor’s edit value.

The “Value” column specifies values displayed in the editor’s edit box.

Note

No additional customization of the RepositoryItemLookUpEditBase.DisplayMember, RepositoryItemLookUpEditBase.KeyMember, and RepositoryItemLookUpEditBase.ValueMember properties is required when the lookup data source is a Dictionary<TKey, TValue> object.

Note

By default, the LookUpEdit control hides the “Key” column by setting its LookUpColumnInfo.Visible property to false.

To access and customize the “Key” and “Value” columns in the LookUpEdit control (for instance, to change the “Value” column caption), use the RepositoryItemLookUpEdit.Columns collection.

Example

This example shows how to set up an in-place lookup editor when its lookup data source is a Dictionary<simpleDataType, simpleDataType> object.In the example, integer values of the CategoryID grid column are edited using an in-place LookUpEdit control (RepositoryItemLookUpEdit). The lookup data source is a Dictionary<int, string> object that associates category IDs with category names.

The following image shows the result:

lookup-dictionary-example-result-animation.gif

using DevExpress.XtraEditors.Repository;
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;

namespace LookUpEdit_Dictionary {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {

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

            Dictionary<int, string> Categories = new Dictionary<int, string>();
            Categories.Add(0, "Beverages");
            Categories.Add(1, "Grains");
            Categories.Add(2, "Seafood");

            RepositoryItemLookUpEdit riLookUp = new RepositoryItemLookUpEdit();
            riLookUp.DataSource = Categories;
            // Change the column caption.
            riLookUp.PopulateColumns();
            riLookUp.Columns["Value"].Caption = "Name";

            gridView1.Columns["CategoryID"].ColumnEdit = riLookUp;
        }
    }

    public class Product {
        public int CategoryID { get; set; }
        public string ProductName { get; set; }
    }


}