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

Dictionary as a Data Source for a Lookup Editor

  • 4 minutes to read

If you use a Dictionary<TKey,TValue> as a data source for a lookup editor, the editor uses keys and values as follows:

  • the TKey values — as actual values. These values are assigned to the editor when a user selects an item in the drop-down box.
  • the TValue values — as string representations of the actual values. These strings are displayed in the edit box and drop-down window.

There is no need to specify the ValueMember and DisplayMember properties.

Note

The TKey and TValue parameters should be of the String type or a value type. For example, keys are integers and values are strings.

The example below uses a Dictionary<TKey,TValue> that contains category identifiers and names.

lookup-dictionary-animation.gif

using System.Collections.Generic;

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

Note that the editor displays a single column, which contains string representations. The column that contains values is hidden.

The Columns property provides access to the column collection. You can use the Key and Value names to retrieve columns in code. The code below shows how to display the Key column and align values in the Value column at the far edge. To specify whether a column is displayed, use the Visible property.

using DevExpress.Utils;

lookUpEdit1.Properties.ForceInitialize();
lookUpEdit1.Properties.PopulateColumns();
lookUpEdit1.Properties.Columns["Key"].Visible = true;
lookUpEdit1.Properties.Columns["Value"].Alignment = HorzAlignment.Far;

Example

In the example below, a grid uses a lookup editor to display and edit values in the Category ID column. The editor is bound to a Dictionary<TKey,TValue> that contains category identifiers and names. The column actually contains integer identifiers but the editor displays category names.

lookup-dictionary-example-result-animation.gif

View Example

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

            gridControl1.RepositoryItems.Add(riLookUp);
            gridView1.Columns["CategoryID"].ColumnEdit = riLookUp;
        }
    }

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


}