gmnnn gmnnn - 1 month ago 19
C# Question

Displaying multiple columns on LookupEdit of Devexpress

I have a

DataSource
bound to a
LookUpEdit
. For example I have 2 columns
FirstName
and
LastName
and I want to set
DisplayMember
property to these two columns.
I found that I should subscribe to
lookUp_CustomDisplayText()
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
e.Value
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()
{
lookUpCompanyPerson.Properties.ForceInitialize();
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

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;
    else
        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:

Comments