ListObject.SetDataBinding, méthode (Object)

Lie un contrôle ListObject à une source de données.

Espace de noms :  Microsoft.Office.Tools.Excel
Assembly :  Microsoft.Office.Tools.Excel (dans Microsoft.Office.Tools.Excel.dll)

Syntaxe

'Déclaration
Sub SetDataBinding ( _
    dataSource As Object _
)
void SetDataBinding(
    Object dataSource
)

Paramètres

Exceptions

Exception Condition
SetDataBindingFailedException

Impossible d'établir une liaison à la source de données spécifiée.

ArgumentException

L'argument n'est pas valide.

ArgumentNullException

L'argument dataSource a la valeur nullune référence null (Nothing en Visual Basic).

Notes

La source de données peut être tout objet qui implémente IList, IListSource, IBindingList ou IEnumerable, tel qu'un DataTable ou qu'un tableau unidimensionnel.

Exemples

L'exemple de code suivant montre comment utiliser la méthode SetDataBinding pour lier un ListObject à un DataTable.DataTable contient deux colonnes, lesquelles contiennent les noms et âges des employés, et quatre lignes qui représentent des entrées des employés.

Cet exemple illustre une personnalisation au niveau du document.

    Private Sub ListObject_SetDataBinding()
        Dim Ages As Integer() = {32, 44, 28, 61}
        Dim Names As String() = {"Reggie", "Sally", _
            "Henry", "Christine"}

        ' Create a data table with two columns.
        Dim table = New DataTable()
        Dim column1 As New DataColumn("Names", GetType(String))
        Dim column2 As New DataColumn("Ages", GetType(Integer))
        table.Columns.Add(column1)
        table.Columns.Add(column2)

        ' Add the four rows of data to the table.
        Dim row As DataRow
        Dim i As Integer
        For i = 0 To 3
            row = table.NewRow()
            row("Names") = Names(i)
            row("Ages") = Ages(i)
            table.Rows.Add(row)
        Next i

        Dim List1 As Microsoft.Office.Tools.Excel.ListObject = _
            Me.Controls.AddListObject(Me.Range("A1", "B4"), "List1")

        ' Bind the list object to the table.
        List1.SetDataBinding(table)

    End Sub

private void ListObject_SetDataBinding()
{
    int[] Ages = { 32, 44, 28, 61 };
    string[] Names = { "Reggie", "Sally", "Henry", "Christine" };

    // Create a data table with two columns.
    System.Data.DataTable table = new DataTable();
    DataColumn column1 = new DataColumn("Names", typeof(string));
    DataColumn column2 = new DataColumn("Ages", typeof(int));
    table.Columns.Add(column1);
    table.Columns.Add(column2);

    // Add the four rows of data to the table.
    DataRow row;
    for (int i = 0; i < 4; i++)
    {
        row = table.NewRow();
        row["Names"] = Names[i];
        row["Ages"] = Ages[i];
        table.Rows.Add(row);
    }

    Microsoft.Office.Tools.Excel.ListObject list1 =
        this.Controls.AddListObject(this.Range["A1", "B4"], "list1");

    // Bind the list object to the table.
    list1.SetDataBinding(table);
}

Sécurité .NET Framework

Voir aussi

Référence

ListObject Interface

SetDataBinding, surcharge

Microsoft.Office.Tools.Excel, espace de noms