Displaying multiple columns on LookupEdit of Devexpress
Solution 1
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:
Solution 2
I've changed Ian O'Brien's code a little bit and it works:
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))
{
DataRowView row = props.GetDataSourceRowByKeyValue(e.Value) as DataRowView;
if (row != null)
{
e.DisplayText = String.Format("{0} {1}", row["FirstName"], row["LastName"]);
}
}
}
Solution 3
i have use it, just like this;
cmb_tip.Properties.DataSource = _dt;
cmb_tip.Properties.ValueMember = "Value";
cmb_tip.Properties.DisplayMember = "Type";
cmb_tip.Properties.PopulateColumns();
cmb_tip.Properties.Columns["Value"].Visible = false;
Solution 4
This is how it works with LookupEditControl in Version 15.2.7 and a Class:
private void lookUpEditPatients_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
var edit = sender as LookUpEdit;
var props = edit.Properties;
var pat = (Patients4ComboBoxVm) props?.GetDataSourceRowByKeyValue(e.Value);
if (pat != null)
{
e.DisplayText = pat.Nachname + ", " + pat.Vorname + "; " + pat.Geburtsdatum + "; " + pat.Versicherungsnummer;
}
}
gmnnn
Updated on July 10, 2022Comments
-
gmnnn almost 2 years
I have a
DataSource
bound to aLookUpEdit
. For example I have 2 columnsFirstName
andLastName
and I want to setDisplayMember
property to these two columns. I found that I should subscribe tolookUp_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: