windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-f5fd38fe.md
Gets or sets the name of the data source field that specifies display text for listbox items. This property is not supported when listbox items are rendered based on Item Templates.
Namespace : DevExpress.XtraEditors
Assembly : DevExpress.XtraEditors.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
[DefaultValue("")]
[DXCategory("Data")]
public virtual string DisplayMember { get; set; }
<DefaultValue("")>
<DXCategory("Data")>
Public Overridable Property DisplayMember As String
| Type | Default | Description |
|---|---|---|
| String | String.Empty |
A string value specifying a field name in a data source.
|
This property is in effect when the ListBox control is bound to a data source with the BaseListBoxControl.DataSource property and listbox items are painted in regular paint mode (Item Templates are not used).
Use the DisplayMember property to specify the field whose values are displayed within the list box control. To get text displayed by a specific list box item, use the BaseListBoxControl.GetItemText method.
Changing the DisplayMember property value at runtime raises the BaseListBoxControl.DisplayMemberChanged event.
The code below shows how to use a DataTable as a data source.
using DevExpress.XtraEditors;
listBoxControl1.DataSource = GetCountries();
listBoxControl1.DisplayMember = "Name";
listBoxControl1.ValueMember = "PhoneCode";
listBoxControl1.SelectedValueChanged += ListBoxControl1_SelectedValueChanged;
private void ListBoxControl1_SelectedValueChanged(object sender, EventArgs e) {
var listBoxControl = sender as ListBoxControl;
XtraMessageBox.Show(listBoxControl.SelectedValue.ToString());
}
DataTable GetCountries() {
DataTable table = new DataTable();
DataColumn name = new DataColumn("Name", typeof(string));
DataColumn phoneCode = new DataColumn("PhoneCode", typeof(int));
table.Columns.AddRange(new DataColumn[] { name, phoneCode});
table.Rows.Add(new object[] { "United States", 1 });
table.Rows.Add(new object[] { "Afghanistan", 93 });
// ...
table.Rows.Add(new object[] { "Zimbabwe", 263 });
return table;
}
Imports DevExpress.XtraEditors
listBoxControl1.DataSource = GetCountries()
listBoxControl1.DisplayMember = "Name"
listBoxControl1.ValueMember = "PhoneCode"
AddHandler listBoxControl1.SelectedValueChanged, AddressOf ListBoxControl1_SelectedValueChanged
Private Sub ListBoxControl1_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim listBoxControl = TryCast(sender, ListBoxControl)
Call XtraMessageBox.Show(listBoxControl.SelectedValue.ToString())
End Sub
Private Function GetCountries() As DataTable
Dim table As DataTable = New DataTable()
Dim name As DataColumn = New DataColumn("Name", GetType(String))
Dim phoneCode As DataColumn = New DataColumn("PhoneCode", GetType(Integer))
table.Columns.AddRange(New DataColumn() {name, phoneCode})
table.Rows.Add(New Object() {"United States", 1})
table.Rows.Add(New Object() {"Afghanistan", 93})
' ...
table.Rows.Add(New Object() {"Zimbabwe", 263})
Return table
End Function
The code below uses the ValueMember and DisplayMember properties to specify data fields that contain a business object’s value and string representation. If you do not specify these properties, the control uses the business object as a value and the ToString() method to obtain the object’s string representation.
using DevExpress.XtraEditors;
listBoxControl1.DataSource = Country.Countries;
listBoxControl1.DisplayMember = "Name";
listBoxControl1.ValueMember = "PhoneCode";
listBoxControl1.SelectedValueChanged += ListBoxControl1_SelectedValueChanged;
private void ListBoxControl1_SelectedValueChanged(object sender, EventArgs e) {
var listBoxControl = sender as ListBoxControl;
XtraMessageBox.Show(listBoxControl.SelectedValue.ToString());
}
public class Country {
public string Name { get; set; }
public int PhoneCode { get; set; }
public override string ToString() {
return $"{Name}, {PhoneCode}";
}
public static Country[] Countries = (new Country[] {
new Country() { Name = "United States", PhoneCode = 1 },
new Country() { Name = "Afghanistan", PhoneCode = 93 },
// ...
new Country() { Name = "Zimbabwe", PhoneCode = 263 }
});
}
Imports DevExpress.XtraEditors
listBoxControl1.DataSource = Country.Countries
listBoxControl1.DisplayMember = "Name"
listBoxControl1.ValueMember = "PhoneCode"
AddHandler listBoxControl1.SelectedValueChanged, AddressOf ListBoxControl1_SelectedValueChanged
Private Sub ListBoxControl1_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim listBoxControl = TryCast(sender, ListBoxControl)
Call XtraMessageBox.Show(listBoxControl.SelectedValue.ToString())
End Sub
Public Class Country
Public Property Name As String
Public Property PhoneCode As Integer
Public Overrides Function ToString() As String
Return $"{Name}, {PhoneCode}"
End Function
Public Shared Countries As Country() = (
New Country() {
New Country() With {
.Name = "United States",
.PhoneCode = 1
},
New Country() With {
.Name = "Afghanistan",
.PhoneCode = 93
},
New Country() With {
.Name = "Zimbabwe",
.PhoneCode = 263
}
}
)
End Class
The following sample code declares a ChangeDisplayMember method that accepts two parameters:
Use this method to assign a single column from a table to the DisplayMember property of the ListBoxControl. If the table bound to the control doesn’t contain the specified column, method execution will not take effect.
using DevExpress.XtraEditors;
// ...
private void ChangeDisplayMember(ListBoxControl listBoxControl, string memberName){
DataTable source = listBoxControl.DataSource as DataTable;
if (source.Columns.Contains(memberName))
listBoxControl.DisplayMember = memberName;
}
Imports DevExpress.XtraEditors
' ...
Private Sub ChangeDisplayMember(ByVal listBoxControl As ListBoxControl, _
ByVal memberName As String)
Dim source As DataTable
source = listBoxControl.DataSource
If source.Columns.Contains(memberName) Then
listBoxControl.DisplayMember = memberName
End If
End Sub
The following code snippets (auto-collected from DevExpress Examples) contain references to the DisplayMember property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
Control.DataSource = dataSource;
Control.DisplayMember = classInfo.DefaultProperty;
foreach (object obj in checkedItems) {
dashboard-constant-lines/CS/ConstantLineExtension.Win/ConstantLineDialog.cs#L40
listBoxControl1.DataSource = customConstantLines;
listBoxControl1.DisplayMember = "Name";
}
Control.DataSource = dataSource
Control.DisplayMember = classInfo.DefaultProperty
For Each obj As Object In checkedItems
See Also