DataGridViewComboBoxColumn.DisplayMember プロパティ

定義

コンボ ボックスに表示する文字列の取得先となるプロパティまたは列を指定する文字列を取得または設定します。Gets or sets a string that specifies the property or column from which to retrieve strings for display in the combo boxes.

public:
 property System::String ^ DisplayMember { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Windows.Forms.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember { get; set; }
member this.DisplayMember : string with get, set
Public Property DisplayMember As String

プロパティ値

String プロパティで指定されたデータ ソース内のプロパティまたは列の名前を指定する DataSourceA String that specifies the name of a property or column in the data source specified in the DataSource property. 既定値は、Empty です。The default is Empty.

属性

例外

CellTemplate プロパティの値が null です。The value of the CellTemplate property is null.

次のコード例は、をDataGridViewComboBoxColumn使用して、title 列のデータ入力を支援する方法を示しています。The following code example demonstrates how to use a DataGridViewComboBoxColumn to aid in data entry of the title column. DisplayMemberは、ユーザーが表示できるテキストを格納するプロパティ名に設定されます。DisplayMember is set to the property name responsible for containing the user-viewable text. この例DisplayMemberでは、マッピングは必要ないため、はValueMemberと同じ値に設定されています。In this example, the DisplayMember is set to the same value as ValueMember because no mapping is necessary. この例は、クラスの概要に関するトピックで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

注釈

オブジェクトのDataGridViewComboBoxCell列に格納されている値のデータは、 DataGridView.DataSourceから取得されます。The data for the values stored in a column of DataGridViewComboBoxCell objects is obtained from the DataGridView.DataSource. このデータが既定以外のプロパティまたは列からのものDisplayMemberである場合は、プロパティを必要なプロパティ名または列名に設定する必要があります。If this data comes from a nondefault property or column, then the DisplayMember property must be set to the necessary property name or column name.

セル値がユーザーに表示されない内部値である場合はDisplayMemberValueMemberおよびを使用して、内部セル値をユーザーが表示できる値にマップします。If the cell values are internal values not visible to the user, then use DisplayMember and ValueMember to map the internal cell values to user-viewable values.

プロパティが文字列配列に設定されている場合DisplayMember 、配列内の各文字列は有効な表示文字列および有効な基になる値として使用されるため、プロパティを設定する必要はありません。 DataSourceWhen the DataSource property is set to a string array, the DisplayMember property does not need to be set because each string in the array will be used as a valid display string and as a valid underlying value.

このプロパティを取得または設定するとDisplayMemberCellTemplateプロパティによって返されるオブジェクトのプロパティが取得または設定されます。Getting or setting this property gets or sets the DisplayMember property of the object returned by the CellTemplate property. また、このプロパティを設定DisplayMemberすると、列のすべてのセルのプロパティが設定され、列の表示が更新されます。Setting this property also sets the DisplayMember property of every cell in the column and refreshes the column display. 個々のセルに対して指定された値をオーバーライドするには、列の値を設定した後にセルの値を設定します。To override the specified value for individual cells, set the cell values after you set the column value.

適用対象

こちらもご覧ください