Back to Devexpress

BaseListBoxControl.ValueMember Property

windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-3b12304f.md

latest7.4 KB
Original Source

BaseListBoxControl.ValueMember Property

Gets or sets the field name in the bound data source whose contents are assigned to item values.

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DefaultValue("")]
[DXCategory("Data")]
public virtual string ValueMember { get; set; }
vb
<DefaultValue("")>
<DXCategory("Data")>
Public Overridable Property ValueMember As String

Property Value

TypeDefaultDescription
StringString.Empty

A string value specifying a field name in the data source.

|

Remarks

This property is in effect when a list box control is bound to a data source via the BaseListBoxControl.DataSource property.

You can optionally specify the ValueMember property for your own purposes. For instance, you can specify this property in order to subsequently get this field’s values for list box items via the BaseListBoxControl.GetItemValue method.

Values of the field specified by the ValueMember property are never displayed.

To specify the field whose values are displayed in the list box control, use the BaseListBoxControl.DisplayMember property.

Changing the ValueMember property value at runtime raises the BaseListBoxControl.ValueMemberChanged event.

Examples

The code below shows how to use a DataTable as a data source.

csharp
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;
}
vb
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.

csharp
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 }
    });
}
vb
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

See Also

DataSource

DisplayMember

ValueMemberChanged

GetItemValue(Int32)

SelectedValue

BaseListBoxControl Class

BaseListBoxControl Members

DevExpress.XtraEditors Namespace