DataGridViewComboBoxCell.DisplayMember Eigenschaft

Definition

Ruft eine Zeichenfolge ab, die angibt, woher in der Dropdownliste anzuzeigende Auswahloptionen entnommen werden sollen, oder legt diese Zeichenfolge fest.Gets or sets a string that specifies where to gather selections to display in the drop-down list.

public:
 virtual property System::String ^ DisplayMember { System::String ^ get(); void set(System::String ^ value); };
public virtual string DisplayMember { get; set; }
member this.DisplayMember : string with get, set
Public Overridable Property DisplayMember As String

Eigenschaftswert

Eine Zeichenfolge, die den Namen einer Eigenschaft oder Spalte in der Datenquelle angibt, die in der DataSource-Eigenschaft angegeben ist.A string specifying the name of a property or column in the data source specified in the DataSource property. Der Standardwert ist Empty. Er gibt an, dass die DisplayMember-Eigenschaft nicht verwendet wird.The default value is Empty, which indicates that the DisplayMember property will not be used.

Ausnahmen

Die DataSource-Eigenschaft ist nicht null, und der angegebene Wert beim Festlegen dieser Eigenschaft ist nicht null oder Empty und benennt keine gültige Eigenschaft oder Spalte in der Datenquelle.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.

Beispiele

Im folgenden Codebeispiel wird die Verwendung der DataGridViewComboBoxColumn.DisplayMember-Eigenschaft veranschaulicht, die dieser Eigenschaft ähnelt.The following code example demonstrates the use of the DataGridViewComboBoxColumn.DisplayMember property, which is similar to this property. Dieses Beispiel ist Teil eines größeren Beispiels, das im Thema Übersicht über DataGridViewComboBoxColumn-Klasse verfügbar ist.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
    };
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
};
Private Function CreateComboBoxColumn() _
    As DataGridViewComboBoxColumn
    Dim column As New DataGridViewComboBoxColumn()

    With column
        .DataPropertyName = ColumnName.TitleOfCourtesy.ToString()
        .HeaderText = ColumnName.TitleOfCourtesy.ToString()
        .DropDownWidth = 160
        .Width = 90
        .MaxDropDownItems = 3
        .FlatStyle = FlatStyle.Flat
    End With
    Return column
End Function

Private Sub SetAlternateChoicesUsingDataSource( _
    ByVal comboboxColumn As DataGridViewComboBoxColumn)
    With comboboxColumn
        .DataSource = RetrieveAlternativeTitles()
        .ValueMember = ColumnName.TitleOfCourtesy.ToString()
        .DisplayMember = .ValueMember
    End With
End Sub

Private Function RetrieveAlternativeTitles() As DataTable
    Return Populate( _
        "SELECT distinct TitleOfCourtesy FROM Employees")
End Function

Private connectionString As String = _
        "Integrated Security=SSPI;Persist Security Info=False;" _
        & "Initial Catalog=Northwind;Data Source=localhost"

Private Function Populate(ByVal sqlCommand As String) As DataTable
    Dim northwindConnection As New SqlConnection(connectionString)
    northwindConnection.Open()

    Dim command As New SqlCommand(sqlCommand, _
        northwindConnection)
    Dim adapter As New SqlDataAdapter()
    adapter.SelectCommand = command
    Dim table As New DataTable()
    table.Locale = System.Globalization.CultureInfo.InvariantCulture
    adapter.Fill(table)

    Return table
End Function

' 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
End Enum

Hinweise

DisplayMember die die Textinformationen darstellt, die in der Dropdown Liste Kombinations Feld angezeigt werden.DisplayMember represents the text information displayed in the combo box drop-down list. Im Gegensatz dazu stellt die ValueMember-Eigenschaft den entsprechenden Wert einer Auswahl dar.In contrast, the ValueMember property represents the corresponding value of a selection.

Wenn die Daten für die von der DataGridViewComboBoxCell angezeigten Auswahl von einer nicht standardmäßigen Eigenschaft oder Spalte der DataSourcegezeichnet werden sollen, müssen DisplayMember zusätzlich zu DataSourcefestgelegt werden.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.

Wenn DataSource auf ein Zeichen folgen Array festgelegt ist, muss DisplayMember nicht festgelegt werden, da jede Zeichenfolge im Array als gültige Anzeige Zeichenfolge und als gültiger zugrunde liegender Wert verwendet wird.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.

Eine weitere Möglichkeit zum Laden der Kombinations Feldauswahl besteht darin, die Items-Eigenschaft zu verwenden.Another way of loading combo box selections is to use the Items property. DisplayMember muss den Eigenschaftsnamen enthalten, aus dem die Auswahl abgerufen werden soll.DisplayMember must contain the property name from which to gather the selections.

Gilt für:

Siehe auch