DataGridViewComboBoxCell.ValueMember プロパティ

定義

ドロップダウン リストで使用する基になる値の収集先を指定する文字列を取得または設定します。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

プロパティ値

プロパティまたは列の名前を指定する文字列。A string specifying the name of a property or column. 既定値は Empty で、このプロパティが無視されることを示します。The default value is Empty, which indicates that this property is ignored.

例外

DataSource プロパティが null ではなく、このプロパティの設定時に指定された値が null または Empty ではなく、データ ソース内の有効なプロパティまたは列の名前でもありません。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.

次のコード例は、 DataGridViewComboBoxColumn.ValueMemberプロパティの使用方法を示しています。このプロパティは、このプロパティに似ています。The following code example demonstrates the use of the DataGridViewComboBoxColumn.ValueMember property, which is similar to this property. この例は、クラスの概要に関するトピックでDataGridViewComboBoxColumn紹介されている大規模な例の一部です。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

注釈

ValueMember選択範囲の対応する値を表します。ValueMember represents the corresponding value of a selection. これに対し、 DisplayMemberプロパティは、ドロップダウンリストに表示されるテキスト情報を表します。In contrast, the DisplayMember property represents the text information displayed in the drop-down list.

プロパティDataSourceは、ドロップダウンリストに表示される選択項目の値のデータソースを指定します。The DataSource property specifies the data source for the value of the selections displayed in the drop-down list.

によってDataGridViewComboBoxCell表示される選択項目のデータが、 DataSourceの既定以外のプロパティまたは列から描画されるValueMemberと想定される場合はDataSource、に加えて、を設定する必要があります。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.

DataSource文字列配列に設定されてValueMemberいる場合は、配列内の各文字列が有効な表示文字列および有効な基になる値として使用されるため、を設定する必要はありません。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.

コンボボックスの選択を読み込むもう1つの方法Itemsは、プロパティを使用することです。Another way of loading combo box selections is to use the Items property. ValueMember選択項目の収集に使用するプロパティ名が含まれている必要があります。ValueMember must contain the property name from which to gather the selections.

適用対象

こちらもご覧ください