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

How to: Add Value Entered by User to LookUpEdit's Data Source

  • 4 minutes to read

Suppose a lookup editor displays a list of persons in the dropdown window. Dropdown rows are retrieved from a collection of a custom ContactList class. For this purpose, the RepositoryItemLookUpEditBase.DataSource property is set to an instance of the ContactList collection.

Each element in the collection represents an object of the Contact class.

The following LookUpEditBase.ProcessNewValue event handler adds new records to the data source. This occurs when the end-user enters a new name in the edit box that does not exist in the dropdown list and presses the Enter key. The entry is added by the Add method implemented in the ContactList class. The ProcessNewValueEventArgs.Handled parameter is set to true in order to force the editor to locate the newly inserted record after the event handler is performed.

Before inserting a new record, a message box is displayed and this allows the user to submit or discard the value. The following message box appears when a ‘Jack Cooper’ entry is entered:

LookupEdit_ProcessNewValue_example_Confirm

After pressing the ‘Yes’ button, the new string is added to the list and the editor picks it up:

LookupEdit_ProcessNewValue_example_EntryAdded

using DevExpress.XtraEditors.Controls;

//ProcessNewValue event handler
private void LookUpEdit1_ProcessNewValue(object sender, ProcessNewValueEventArgs e) {
    if ((string)e.DisplayValue != String.Empty &&
        MessageBox.Show(this, "Add the '" + e.DisplayValue.ToString() + "' entry to the list?",
        "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) {
        ((sender as LookUpEdit).Properties.DataSource as ContactList).Add(
          new Contact(e.DisplayValue.ToString()));
        e.Handled = true;
    }
}

private void Form1_Load(object sender, System.EventArgs e) {
    //create a contact list 
    ContactList cList = new ContactList();
    cList.Add(new Contact("John Doe"));
    cList.Add(new Contact("Sam Hill"));
    cList.Add(new Contact("Karen Holmes"));
    cList.Add(new Contact("Bobbie Valentine"));
    cList.Add(new Contact("Frank Frankson"));

    //bind the lookup editor to the list
    lookUpEdit1.Properties.DataSource = cList;
    lookUpEdit1.Properties.DisplayMember = "Name";
    lookUpEdit1.Properties.ValueMember = "ID";            
    // Add columns.
    // The ID column is populated 
    // via the GetNotInListValue event (not listed in the example).
    lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("ID", "ID", 20));
    lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("Name", "Name", 80));
    //enable text editing
    lookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard
}


public class ContactList : System.Collections.CollectionBase {
    public Contact this[int index] {
        get {return (Contact)(List[index]);}
        set {List[index] = value;}
    }

    public int Add(Contact value) {
        return List.Add(value);
    }
    //...
}


 public class Contact {
     private string name;
     public Contact(string _name) {
         name = _name;
     }

     public string Name
     {
         get { return name; }
         set { name = value; }
     }

     public string Id
     {
         get { return Id; }
         set { name = value; }
     }
 }