DataGrid.DataSource Proprietà

Definizione

Ottiene o imposta l'origine dati per la quale la griglia visualizza i dati.

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

Valore della proprietà

Oggetto che funge da origine dati.

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come impostare DataSourcee, quando necessario, per DataMemberassociare un System.Windows.Forms.DataGrid oggetto a e DataView a .DataSet L'esempio mostra anche come restituire origini dati da 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

Commenti

In fase di esecuzione, usare il SetDataBinding metodo per impostare le DataSource proprietà e DataMember .

Le origini dati seguenti sono valide:

Per altre informazioni sulle origini dati, vedere panoramica della Binding classe.

Se il DataSource riferimento contiene più tabelle, è necessario impostare la DataMember proprietà una stringa che specifica la tabella a cui eseguire l'associazione. Ad esempio, se è DataSource un oggetto o DataViewManager che contiene tre tabelle denominate Customers, Orderse OrderDetails, è necessario specificare la tabella a cui eseguire DataSet l'associazione.

Se si imposta su DataSource un oggetto che non implementa l'interfaccia IList o un oggetto IListSource , la griglia genererà un'eccezione.

È possibile creare una griglia che consente agli utenti di modificare i dati, ma impedisce loro di aggiungere nuove righe usando come DataView origine dati e impostando la AddNew proprietà su false.

Per associare l'oggetto DataGrid a una matrice fortemente tipizzata di oggetti, il tipo di oggetto deve contenere proprietà pubbliche. Per creare un DataGridTableStyle oggetto che visualizza la matrice, impostare la DataGridTableStyle.MappingName proprietà su typename dove typename viene sostituito dal nome del tipo di oggetto. Si noti anche che la MappingName proprietà fa distinzione tra maiuscole e minuscole. Il nome del tipo deve corrispondere esattamente. Per un esempio, vedere la MappingName proprietà .

È anche possibile associare l'oggetto DataGrid a un oggetto ArrayList. Una funzionalità di ArrayList è che può contenere oggetti di più tipi, ma può DataGrid essere associato solo a tale elenco quando tutti gli elementi dell'elenco sono dello stesso tipo del primo elemento. Ciò significa che tutti gli oggetti devono essere dello stesso tipo oppure devono ereditare dalla stessa classe del primo elemento dell'elenco. Ad esempio, se il primo elemento di un elenco è , Controlil secondo elemento può essere un oggetto TextBox (che eredita da Control). Se, d'altra parte, il primo elemento è un TextBoxoggetto , il secondo oggetto non può essere un oggetto Control. Inoltre, deve ArrayList avere elementi al suo interno quando è associato. Un valore vuoto ArrayList comporterà una griglia vuota. Inoltre, gli oggetti in ArrayList devono contenere proprietà pubbliche. Quando si esegue l'associazione a un ArrayListoggetto , impostare l'oggetto MappingNameDataGridTableStyle di su "ArrayList" (il nome del tipo).

Si applica a

Vedi anche