DataGridViewComboBoxCell.DisplayMember プロパティ

定義

ドロップダウン リストに表示する選択項目の収集先を指定する文字列を取得または設定します。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

プロパティ値

DataSource プロパティで指定されたデータ ソース内のプロパティまたは列の名前を指定する文字列。A string specifying the name of a property or column in the data source specified in the DataSource property. 既定値は Empty で、DisplayMember プロパティが使用されないことを示します。The default value is Empty, which indicates that the DisplayMember property will not be used.

例外

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.DisplayMemberプロパティの使用方法を示しています。このプロパティは、このプロパティに似ています。The following code example demonstrates the use of the DataGridViewComboBoxColumn.DisplayMember 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

注釈

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

によってDataGridViewComboBoxCell表示される選択項目のデータが、 DataSourceの既定以外のプロパティまたは列から描画されるDisplayMemberと想定される場合は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 DisplayMember must be set in addition to DataSource.

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

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

適用対象

こちらもご覧ください