Skip to main content

How to: Create Column with Custom Data in LookUpEdit

  • 4 minutes to read

The code below shows how to make a lookup editor display data from a custom array. The editor contains two columns. One column is bound to a field from the data source. The other column is unbound and is populated via the RepositoryItemLookUpEdit.GetNotInListValue event.

The data source of a lookup editor represents an array of Record class objects. The Record class declares the public Country property whose values are displayed in the column of the same name. The lookup editor also contains an ID column. Its field name (“ID”) does not coincide with any fields from the data source. So this column is unbound and is populated via the RepositoryItemLookUpEdit.GetNotInListValue event. In the event handler, we return the record id as a value for the column.

The following image shows the lookup editor after running this code:

LookUpEdit_GetNotInList_example

using DevExpress.XtraEditors.Controls;

//The class representing a row in a lookup editor
public class Record {
    private string country;
    public Record(string country) {
        this.country = country;
    }
    public string Country {
        get { return country; }
        set { country = value; }
    }
}

// ...
// An array of records displayed in the dropdown in the lookup editor
Record[] records = null;

//Fill the records array
private void InitData() {
    string[] data = (new string[] {"United States", "Afghanistan", "Albania", "Algeria",
  "Andorra", "Angola"});
    records = new Record[data.Length];
    for (int i = 0; i < data.Length; i++)
        records[i] = new Record(data[i]);
}

//Set up the lookup editor
private void InitLookUp() {
    //Specify an array of countries as a data source
    lookUpEdit2.Properties.DataSource = records;
    //The field whose values are displayed in the edit box
    lookUpEdit2.Properties.DisplayMember = "Country";
    //The field whose values match the edit value
    lookUpEdit2.Properties.ValueMember = "Country";

    //Unbound column
    lookUpEdit2.Properties.Columns.Add(new LookUpColumnInfo("ID", "ID", 20));
    //Column bound to the existing 'Country' field from the data source
    lookUpEdit2.Properties.Columns.Add(new LookUpColumnInfo("Country", "Country", 100));

    //Select the first record from the dropdown
    lookUpEdit2.EditValue = records[0].Country;
}


private void Form1_Load(object sender, System.EventArgs e) {
    InitData();
    InitLookUp();
}

private void lookUpEdit2_GetNotInListValue(object sender, GetNotInListValueEventArgs e) 
    //provide a value for the ID field
    if (e.FieldName == "ID")
        e.Value = e.RecordIndex.ToString() + ".";
}
See Also