Share via


Proprietà ListObject.DataSource

Ottiene o imposta l'origine che contiene un elenco di valori utilizzati per popolare gli elementi all'interno del controllo.

Spazio dei nomi:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

Sintassi

'Dichiarazione
Property DataSource As Object
Object DataSource { get; set; }

Valore proprietà

Tipo: System.Object
Origine che contiene un elenco di valori utilizzati per popolare gli elementi all'interno del controllo.Il valore predefinito è nullriferimento null (Nothing in Visual Basic).

Note

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

Di seguito sono riportate le origini dati valide:

Per ulteriori informazioni sulle origini dati, vedere i cenni preliminari sulla classe Binding.

Se il riferimento alla proprietà DataSource contiene più di una tabella o di una proprietà, è necessario impostare la proprietà DataMember su una stringa in cui è specificata la tabella cui deve essere associato. Se ad esempio la proprietà DataSource è impostata su un oggetto DataSet o DataViewManager che contiene tre tabelle denominate Clienti, Ordini e DettagliOrdine, è necessario specificare la tabella da utilizzare per l'associazione.

Se la proprietà DataSource viene impostata su un oggetto che non implementa l'interfaccia IList o un oggetto IListSource, verrà generata un'eccezione.

Esempi

Nell'esempio di codice riportato di seguito viene creato un oggetto DataSet con due oggetti DataTable e una delle tabelle viene compilata con dati. Vengono quindi impostate le proprietà DataSource e DataMember del controllo ListObject da associare alla tabella che contiene i dati.

Questo esempio è valido per una personalizzazione a livello di documento.

    Private Sub ListObject_DataSourceAndMember()
        ' Create a DataSet and two DataTables.
        Dim ordersDataSet As New DataSet("ordersDataSet")
        Dim tableCustomers As New DataTable("Customers")
        Dim tableProducts As New DataTable("Products")
        ordersDataSet.Tables.Add(tableCustomers)
        ordersDataSet.Tables.Add(tableProducts)

        ' Add a data to the Customers DataTable.
        tableCustomers.Columns.Add(New DataColumn("LastName"))
        tableCustomers.Columns.Add(New DataColumn("FirstName"))
        Dim dr As DataRow = tableCustomers.NewRow()
        dr("LastName") = "Chan"
        dr("FirstName") = "Gareth"
        tableCustomers.Rows.Add(dr)

        ' Create a list object.
        Dim List1 As Microsoft.Office.Tools.Excel.ListObject = _
            Me.Controls.AddListObject(Me.Range( _
            "A1"), "Customers")

        ' Bind the list object to the Customers table.
        List1.AutoSetDataBoundColumnHeaders = True
        List1.DataSource = ordersDataSet
        List1.DataMember = "Customers"

    End Sub

private void ListObject_DataSourceAndMember()
{
    // Create a DataSet and two DataTables.
    DataSet ordersDataSet = new DataSet("ordersDataSet");
    DataTable tableCustomers = new DataTable("Customers");
    DataTable tableProducts = new DataTable("Products");
    ordersDataSet.Tables.Add(tableCustomers);
    ordersDataSet.Tables.Add(tableProducts);

    // Add a data to the Customers DataTable.
    tableCustomers.Columns.Add(new DataColumn("LastName"));
    tableCustomers.Columns.Add(new DataColumn("FirstName"));
    DataRow dr = tableCustomers.NewRow();
    dr["LastName"] = "Chan";
    dr["FirstName"] = "Gareth";
    tableCustomers.Rows.Add(dr);

    // Create a list object.
    Microsoft.Office.Tools.Excel.ListObject list1 = 
        this.Controls.AddListObject(
        this.Range["A1"], "Customers");

    // Bind the list object to the Customers table.
    list1.AutoSetDataBoundColumnHeaders = true;
    list1.DataSource = ordersDataSet;
    list1.DataMember = "Customers";
}

Sicurezza di .NET Framework

Vedere anche

Riferimenti

ListObject Interfaccia

Spazio dei nomi Microsoft.Office.Tools.Excel