DataGridViewComboBoxCell.ValueMember Property

Definition

Gets or sets a string that specifies where to gather the underlying values used in the drop-down list.

C#
public virtual string ValueMember { get; set; }

Property Value

A string specifying the name of a property or column. The default value is Empty, which indicates that this property is ignored.

Exceptions

The DataSource property is not null and the specified value when setting this property is not null or Empty and does not name a valid property or column in the data source.

Examples

The following code example demonstrates the use of the DataGridViewComboBoxColumn.ValueMember property, which is similar to this property. This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

C#
private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
    DataGridViewComboBoxColumn column =
        new DataGridViewComboBoxColumn();
    {
        column.DataPropertyName = ColumnName.TitleOfCourtesy.ToString();
        column.HeaderText = ColumnName.TitleOfCourtesy.ToString();
        column.DropDownWidth = 160;
        column.Width = 90;
        column.MaxDropDownItems = 3;
        column.FlatStyle = FlatStyle.Flat;
    }
    return column;
}

private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
{
    {
        comboboxColumn.DataSource = RetrieveAlternativeTitles();
        comboboxColumn.ValueMember = ColumnName.TitleOfCourtesy.ToString();
        comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
    }
}

private DataTable RetrieveAlternativeTitles()
{
    return Populate("SELECT distinct TitleOfCourtesy FROM Employees");
}

string connectionString =
    "Integrated Security=SSPI;Persist Security Info=False;" +
    "Initial Catalog=Northwind;Data Source=localhost";

private DataTable Populate(string sqlCommand)
{
    SqlConnection northwindConnection = new SqlConnection(connectionString);
    northwindConnection.Open();

    SqlCommand command = new SqlCommand(sqlCommand, northwindConnection);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = command;

    DataTable table = new DataTable();
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    adapter.Fill(table);

    return table;
}

// Using an enum provides some abstraction between column index
// and column name along with compile time checking, and gives
// a handy place to store the column names.
enum ColumnName
{
    EmployeeId,
    LastName,
    FirstName,
    Title,
    TitleOfCourtesy,
    BirthDate,
    HireDate,
    Address,
    City,
    Region,
    PostalCode,
    Country,
    HomePhone,
    Extension,
    Photo,
    Notes,
    ReportsTo,
    PhotoPath,
    OutOfOffice
};

Remarks

ValueMember represents the corresponding value of a selection. In contrast, the DisplayMember property represents the text information displayed in the drop-down list.

The DataSource property specifies the data source for the value of the selections displayed in the drop-down list.

If the data for the selections displayed by the DataGridViewComboBoxCell is supposed to be drawn from a nondefault property or column of the DataSource, then ValueMember must be set in addition to DataSource.

When DataSource is set to a string array, ValueMember does not need to be set because each string in the array will be used as a valid display string and a valid underlying value.

Another way of loading combo box selections is to use the Items property. ValueMember must contain the property name from which to gather the selections.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also