DataGridViewComboBoxCell.DataSource プロパティ

定義

ドロップダウン リストに表示する選択項目に関するデータを含むデータ ソースを取得または設定します。Gets or sets the data source whose data contains the possible selections shown in the drop-down list.

public:
 virtual property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public virtual object DataSource { get; set; }
member this.DataSource : obj with get, set
Public Overridable Property DataSource As Object

プロパティ値

ドロップダウン リストにデータを提供するために使用する値のコレクションを格納する IList または IListSourceAn IList or IListSource that contains a collection of values used to supply data to the drop-down list. 既定値は null です。The default value is null.

例外

このプロパティの設定時に指定された値が null ではなく、IList 型または IListSource 型でもありません。The specified value when setting this property is not null and is not of type IList nor IListSource.

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

注釈

通常、このプロパティは、 DataGridViewComboBoxColumn.DataSourceプロパティを使用してセルの列全体に対して設定されます。Typically this property is set for an entire column of cells through the DataGridViewComboBoxColumn.DataSource property.

可能であればDataSource 、選択した列のように、選択可能な選択のみを含むソースに設定します。If possible, set DataSource to a source containing only the possible selections, like a column of selections. その場合DisplayMember 、プロパティを設定する必要はありません。Then the DisplayMember property doesn't need to be set. ただし、ソースがより複雑な場合はDisplayMember 、使用可能な選択を取得するプロパティまたは列の名前をに設定します。But if the source is more complicated, set DisplayMember to the name of the property or column from which to retrieve possible selections.

DataSource文字列配列ValueMemberに設定されている場合DisplayMemberは、配列内の各文字列が値と表示の両方に使用されるため、とを設定する必要はありません。If DataSource is set to a string array, then ValueMember and DisplayMember do not need to be set because each string in the array will be used for both value and display.

プロパティを変更すると、セルのItemsコレクションが再初期化され、それ自体が再描画されます。 DataSourceChanging the DataSource property causes the cell to reinitialize its Items collection and redraw itself.

DataSourceプロパティの値を変更すると、コントロールは、新しいデータソースでValueMemberプロパティDisplayMember値とプロパティ値を使用しようとします。またnull 、値が見つからない場合は、各プロパティをに設定します。When you change the value of the DataSource property, the control attempts to use the ValueMember and DisplayMember property values with the new data source, and sets each property to null if its value cannot be found. このプロセス中に発生NullReferenceExceptionした例外はOutOfMemoryExceptionStackOverflowException、、、 IndexOutOfRangeException ThreadAbortException ExecutionEngineException、、、またはAccessViolationExceptionのいずれかの重大な例外でない限り無視されます。Any exception that occurs during this process is ignored unless it is one of the following critical exceptions: NullReferenceException, StackOverflowException, OutOfMemoryException, ThreadAbortException, ExecutionEngineException, IndexOutOfRangeException, or AccessViolationException.

を設定Disposedすると、がデータソースのイベントにDataGridViewComboBoxCellアタッチDataSourceされます。When the DataSource is set, the DataGridViewComboBoxCell attaches to the Disposed event of the data source. DataGridViewComboBoxCellDataGridViewからデタッチされると、リソースはすぐには解放されません。When the DataGridViewComboBoxCell is detached from the DataGridView, the resource is not immediately released. DataGridViewComboBoxCellリソースは、アタッチされたデータソースが破棄されると解放されます。The DataGridViewComboBoxCell resource is released when the attached data source is disposed. ガベージコレクションのためにリソースをすぐに解放するDataSourceにはnull、プロパティをに設定します。To release the resource for garbage collection immediately, set the DataSource property to null.

適用対象

こちらもご覧ください