DataGridViewComboBoxColumn.DisplayMember Property

Gets or sets a string that specifies the property or column from which to retrieve strings for display in the combo boxes.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember { get; set; }

Property Value

Type: System.String
A String that specifies the name of a property or column in the data source specified in the DataSource property. The default is String.Empty.

ExceptionCondition
InvalidOperationException

The value of the CellTemplate property is null.

The data for the values stored in a column of DataGridViewComboBoxCell objects is obtained from the DataGridView.DataSource. If this data comes from a nondefault property or column, then the DisplayMember property must be set to the necessary property name or column name.

If the cell values are internal values not visible to the user, then use DisplayMember and ValueMember to map the internal cell values to user-viewable values.

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

Getting or setting this property gets or sets the DisplayMember property of the object returned by the CellTemplate property. Setting this property also sets the DisplayMember property of every cell in the column and refreshes the column display. To override the specified value for individual cells, set the cell values after you set the column value.

The following code example demonstrates how to use a DataGridViewComboBoxColumn to aid in data entry of the title column. DisplayMember is set to the property name responsible for containing the user-viewable text. In this example, the DisplayMember is set to the same value as ValueMember because no mapping is necessary. This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

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
};

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft