DataGrid.DataSource Właściwość

Definicja

Pobiera lub ustawia źródło danych, dla którego siatka wyświetla dane.

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

Wartość właściwości

Object

Obiekt, który działa jako źródło danych.

Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano, jak ustawić DataSourceelement , i w razie potrzeby , DataMemberaby powiązać System.Windows.Forms.DataGrid element z elementem DataView i DataSet. W przykładzie pokazano również, jak zwracać źródła danych z elementu 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

Uwagi

W czasie wykonywania użyj SetDataBinding metody , aby ustawić DataSource właściwości i DataMember .

Następujące źródła danych są prawidłowe:

Zobacz omówienie klasy, Binding aby uzyskać więcej informacji na temat źródeł danych.

DataSource Jeśli odwołanie zawiera więcej niż jedną tabelę, należy ustawić DataMember właściwość ciąg określający tabelę do powiązania. Jeśli na przykład element DataSource zawiera DataViewManager DataSet trzy tabele o nazwie Customers, Ordersi OrderDetails, należy określić tabelę, z którą ma zostać powiązana.

Ustawienie obiektu DataSource na obiekt, który nie implementuje interfejsu IList lub IListSource spowoduje, że siatka zgłosi wyjątek.

Możesz utworzyć siatkę, która umożliwia użytkownikom edytowanie danych, ale uniemożliwia dodawanie nowych wierszy przy użyciu elementu DataView jako źródła danych i ustawiania AddNew właściwości na falsewartość .

Aby powiązać obiekt z DataGrid silnie typizowanym tablicą obiektów, typ obiektu musi zawierać właściwości publiczne. Aby utworzyć obiekt DataGridTableStyle wyświetlający tablicę, ustaw DataGridTableStyle.MappingName właściwość na typename gdzie typename jest zastępowana przez nazwę typu obiektu. Należy również pamiętać, że MappingName właściwość uwzględnia wielkość liter. Nazwa typu musi być dokładnie dopasowana. Zapoznaj się z MappingName właściwością, aby zapoznać się z przykładem.

Można również powiązać element z DataGrid elementem ArrayList. Funkcja ArrayList jest taka, że może zawierać obiekty wielu typów, ale DataGrid może wiązać się tylko z taką listą, gdy wszystkie elementy na liście są tego samego typu co pierwszy element. Oznacza to, że wszystkie obiekty muszą być tego samego typu lub muszą dziedziczyć z tej samej klasy co pierwszy element na liście. Jeśli na przykład pierwszy element na liście to Control, drugi element może być elementem TextBox (który dziedziczy z Controlelementu ). Jeśli z drugiej strony pierwszy element to TextBox, drugi obiekt nie może być obiektem Control. Ponadto element ArrayList musi zawierać elementy, gdy jest powiązany. Pusta wartość spowoduje pustą ArrayList siatkę. Ponadto obiekty w obiekcie ArrayList muszą zawierać właściwości publiczne. Po powiązaniu z elementem ArrayListustaw dla MappingName DataGridTableStyle elementu wartość "ArrayList" (nazwa typu).

Dotyczy

Zobacz też