DataGridViewComboBoxCell::DisplayMember Property

 

Gets or sets a string that specifies where to gather selections to display in the drop-down list.

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

public:
property String^ DisplayMember {
	virtual String^ get();
	virtual void set(String^ value);
}

Property Value

Type: System::String^

A string specifying the name of a property or column in the data source specified in the DataSource property. The default value is String::Empty, which indicates that the DisplayMember property will not be used.

Exception Condition
ArgumentException

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

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

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 DisplayMember must be set in addition to DataSource.

When DataSource is set to a string array, DisplayMember 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. DisplayMember must contain the property name from which to gather the selections.

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

private:
    DataGridViewComboBoxColumn^ CreateComboBoxColumn()
    {
        DataGridViewComboBoxColumn^ column =
            gcnew 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;

private:
    DataTable^ Populate(String^ sqlCommand)
    {
        SqlConnection^ northwindConnection = gcnew SqlConnection(connectionString);
        northwindConnection->Open();

        SqlCommand^ command = gcnew SqlCommand(sqlCommand, northwindConnection);
        SqlDataAdapter^ adapter = gcnew SqlDataAdapter();
        adapter->SelectCommand = command;

        DataTable^ table = gcnew DataTable();
        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 class ColumnName
    {
        EmployeeID,
        LastName,
        FirstName,
        Title,
        TitleOfCourtesy,
        BirthDate,
        HireDate,
        Address,
        City,
        Region,
        PostalCode,
        Country,
        HomePhone,
        Extension,
        Photo,
        Notes,
        ReportsTo,
        PhotoPath,
        OutOfOffice
    };

.NET Framework
Available since 2.0
Return to top
Show: