DataGridViewComboBoxCell.ValueMember Eigenschaft

Definition

Ruft eine Zeichenfolge ab, die angibt, woher die in der Dropdownliste zu verwendenden Werte entnommen werden sollen, oder legt diese Zeichenfolge fest.Gets or sets a string that specifies where to gather the underlying values used in the drop-down list.

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

Eigenschaftswert

Eine Zeichenfolge, die den Namen einer Eigenschaft oder einer Spalte angibt.A string specifying the name of a property or column. Der Standardwert ist Empty und gibt an, dass diese Eigenschaft ignoriert wird.The default value is Empty, which indicates that this property is ignored.

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.ValueMember-Eigenschaft veranschaulicht, die dieser Eigenschaft ähnelt.The following code example demonstrates the use of the DataGridViewComboBoxColumn.ValueMember 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

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

Die DataSource-Eigenschaft gibt die Datenquelle für den Wert der in der Dropdown Liste angezeigten Auswahl an.The DataSource property specifies the data source for the value of the selections displayed in the drop-down list.

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

Wenn DataSource auf ein Zeichen folgen Array festgelegt ist, muss ValueMember 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, 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.

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. ValueMember muss den Eigenschaftsnamen enthalten, aus dem die Auswahl abgerufen werden soll.ValueMember must contain the property name from which to gather the selections.

Gilt für:

Siehe auch