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

屬性值

IListIListSource,包含用來提供資料給此下拉式清單的值集合。An IList or IListSource that contains a collection of values used to supply data to the drop-down list. 預設值為 nullThe default value is null.

例外狀況

當設定這個屬性時的指定值不是 null,也不具備型別 IListIListSourceThe 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設定為字串陣列,則ValueMemberDisplayMember不需要設定,因為陣列中的每個字串都會同時用於值和顯示。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例外StackOverflowException狀況OutOfMemoryException:、 ExecutionEngineExceptionIndexOutOfRangeExceptionThreadAbortExceptionAccessViolationException、或。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.

當設定時DataGridViewComboBoxCellDisposed會附加至資料來源的事件。 DataSourceWhen the DataSource is set, the DataGridViewComboBoxCell attaches to the Disposed event of the data source. 當從卸離時,不會立即釋放資源。DataGridViewComboBoxCell DataGridViewWhen 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.

適用於

另請參閱