gmnnn gmnnn - 1 year ago 646
C# Question

Displaying multiple columns on LookupEdit of Devexpress

I have a

bound to a
. For example I have 2 columns
and I want to set
property to these two columns.
I found that I should subscribe to
and edit display text property like this:

private void lookUpCompanyPerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
LookUpEdit edit = sender as LookUpEdit;

if (e.DisplayText != "")
e.DisplayText = e.DisplayText + " " + (string)e.Value;

but I did not understand what
is and I want to display another column for selected row, not the valuemember of selected row.

This is how I bind the datasource to lookupedit:

private void populateComboBoxForCompanyPerson()
bs = new BindingSource(myDataSet, "CompanyPerson");
lookUpCompanyPerson.Properties.DataSource = bs;
lookUpCompanyPerson.Properties.DisplayMember = "CompanyName";
lookUpCompanyPerson.Properties.ValueMember = "PersonID";
this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("PersonID"));
this.lookUpCompanyPerson.Properties.Columns["PersonID"].Visible = false;
this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("FirstName"));
this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("LastName"));
this.lookUpCompanyPerson.Properties.Columns.Add(new LookUpColumnInfo("CompanyName"));

And this is what my datasource looks like: datasource

Answer Source

From the DevExpress documentation:

  • e.Value gets or sets editor's current value.
  • e.DisplayText gets or sets an editor's display text

The lookup editor's value is obtained from the data source field specified by the RepositoryItemLookUpEditBase.ValueMember property. The GetDataSourceRowByKeyValue method searches for the specified value within this field and returns an object representing the first found record.

The GetDataSourceRowByKeyValue method's return value depends upon the type of the underlying data source. If the data source is a System.Data.DataTable or a System.Data.DataView, this method returns a System.Data.DataRowView object. If the data source is a custom list of items, the appropriate list item is returned.

You want to set the e.Value to the value that you want to display in the control.

private void lookUpCompanyPerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
    RepositoryItemLookUpEdit props
    if (sender is LookUpEdit)
        props = (sender as LookUpEdit).Properties;
        props = sender as RepositoryItemLookUpEdit;
    if (props != null && (e.Value is int))
        object row = props.GetDataSourceRowByKeyValue(e.Value);
        if (row != null)
            e.Value = String.Format("{0} {1}", (DataRowView)row["FirstName"], (DataRowView)row["LastName"]);
            e.Handled = true;

Finally, here are some useful pages with more documentation:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download