DataGrid.DataSource 屬性

定義

取得或設定在方格中顯示資料的資料來源。

public:
 property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public object DataSource { get; set; }
public object DataSource { get; set; }
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DataSource : obj with get, set
member this.DataSource : obj with get, set
Public Property DataSource As Object

屬性值

當做資料來源的物件。

屬性

範例

下列程式碼範例示範如何設定 DataSource ,以及在需要時, DataMember 將 系結 System.Windows.Forms.DataGridDataViewDataSet 。 此範例也會示範如何從 System.Windows.Forms.DataGrid 傳回資料來源。

private:
   void BindToDataView( DataGrid^ myGrid )
   {
      // Create a DataView using the DataTable.
      DataTable^ myTable = gcnew DataTable( "Suppliers" );
      // Insert code to create and populate columns.
      DataView^ myDataView = gcnew DataView( myTable );
      myGrid->DataSource = myDataView;
   }

   void BindToDataSet( DataGrid^ myGrid )
   {
      // Create a DataSet.
      DataSet^ myDataSet = gcnew DataSet( "myDataSet" );
      // Insert code to populate DataSet with several tables.
      myGrid->DataSource = myDataSet;
      // Use the DataMember property to specify the DataTable.
      myGrid->DataMember = "Suppliers";
   }

   DataView^ GetDataViewFromDataSource()
   {
      // Create a DataTable variable, and set it to the DataSource.
      DataView^ myDataView;
      myDataView = (DataView^)(dataGrid1->DataSource);
      return myDataView;
   }

   DataSet^ GetDataSetFromDataSource()
   {
      // Create a DataSet variable, and set it to the DataSource.
      DataSet^ myDataSet;
      myDataSet = (DataSet^)(dataGrid1->DataSource);
      return myDataSet;
   }
private void BindToDataView(DataGrid myGrid){
    // Create a DataView using the DataTable.
    DataTable myTable = new DataTable("Suppliers");
    // Insert code to create and populate columns.
    DataView myDataView = new DataView(myTable);
    myGrid.DataSource = myDataView;
 }
 private void BindToDataSet(DataGrid myGrid){
    // Create a DataSet.
    DataSet myDataSet = new DataSet("myDataSet");
    // Insert code to populate DataSet with several tables.
    myGrid.DataSource = myDataSet;
    // Use the DataMember property to specify the DataTable.
    myGrid.DataMember = "Suppliers";
 }
 private DataView GetDataViewFromDataSource(){
    // Create a DataTable variable, and set it to the DataSource.
    DataView myDataView;
    myDataView = (DataView) dataGrid1.DataSource;
    return myDataView;
 }
 private DataSet GetDataSetFromDataSource(){
    // Create a DataSet variable, and set it to the DataSource.
    DataSet myDataSet;
    myDataSet = (DataSet) dataGrid1.DataSource;
    return myDataSet;
 }
Private Sub BindToDataView(myGrid As DataGrid)
    ' Create a DataView using the DataTable.
    Dim myTable As New DataTable("Suppliers")
    ' Insert code to create and populate columns.
    Dim myDatatView As New DataView(myTable)
    myGrid.DataSource = myDatatView
End Sub

Private Sub BindToDataSet(myGrid As DataGrid)
    ' Create a DataSet.
    Dim myDataSet As New DataSet("myDataSet")
    ' Insert code to populate DataSet with several tables.
    myGrid.DataSource = myDataSet
    ' Use the DataMember property to specify the DataTable.
    myGrid.DataMember = "Suppliers"
End Sub

Private Function GetDataViewFromDataSource() As DataView
    ' Create a DataTable variable, and set it to the DataSource.
    Dim myDatatView As DataView
    myDatatView = CType(dataGrid1.DataSource, DataView)
    Return myDatatView
End Function 'GetDataViewFromDataSource

Private Function GetDataSetFromDataSource() As DataSet
    ' Create a DataSet variable, and set it to the DataSource.
    Dim myDataSet As DataSet
    myDataSet = CType(dataGrid1.DataSource, DataSet)
    Return myDataSet
End Function 'GetDataSetFromDataSource

備註

在執行時間,使用 SetDataBinding 方法來設定 DataSourceDataMember 屬性。

下列資料來源有效:

Binding如需資料來源的詳細資訊,請參閱 類別概觀。

DataSource如果參考包含一個以上的資料表,您必須將屬性設定 DataMember 為指定要系結之資料表的字串。 例如,如果 DataSource 是 或 DataViewManager ,其中包含三個名為 DataSetCustomersOrdersOrderDetails 的資料表,您必須指定要系結的資料表。

DataSource將 設定為未實 IList 作 介面的物件,或 IListSource 會導致方格擲回例外狀況。

您可以建立一個方格,讓使用者編輯資料,但防止他們新增資料列,方法是使用 DataView 做為資料來源,並將 屬性設定 AddNewfalse

若要將 系結 DataGrid 至物件的強型別陣列,物件類型必須包含公用屬性。 若要建立 DataGridTableStyle 顯示陣列的 ,請將 DataGridTableStyle.MappingName 屬性設定為 typename ,其中 typename 會以物件類型的名稱取代。 另請注意, MappingName 屬性區分大小寫;類型名稱必須完全符合。 如需範例, MappingName 請參閱 屬性。

您也可以將 DataGrid 系結至 ArrayList 。 的一項功能 ArrayList 是它可以包含多個類型的物件,但 DataGrid 只有在清單中的所有專案都與第一個專案的類型相同時,才能系結至這類清單。 這表示所有物件都必須是相同的類型,或者它們必須繼承自與清單中第一個專案相同的類別。 例如,如果清單中的第一個專案是 Control ,則第二個專案可能是 TextBox 繼承自 Control) (。 如果另一方面,第一個專案是 ,則第二個 TextBox 物件不能是 Control 。 此外,系 ArrayList 結時,必須有其中的專案。 空白 ArrayList 會導致空的方格。 此外,中的 ArrayList 物件必須包含公用屬性。 當系結至 ArrayList 時,請將 的 DataGridTableStyle 設定 MappingName 為 「ArrayList」 (類型名稱) 。

適用於

另請參閱