ListObject.SetDataBinding, méthode (Object, String, array<String )

Lie un contrôle ListObject à une donnée membre spécifiée d'une source de données, et affiche uniquement les colonnes spécifiées de cette donnée membre.

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, _
    dataMember As String, _
    ParamArray mappedColumns As String() _
)
void SetDataBinding(
    Object dataSource,
    string dataMember,
    params string[] mappedColumns
)

Paramètres

  • mappedColumns
    Type : array<System.String[]
    Noms des colonnes de la donnée membre que vous souhaitez afficher dans le contrôle ListObject.Pour ajouter une colonne non mappée, incluez une chaîne vide dans le tableau.

Exceptions

Exception Condition
SetDataBindingFailedException

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

ArgumentException

Un ou plusieurs arguments ne sont pas valides.

ArgumentNullException

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

Notes

Le paramètre dataSource peut être tout objet qui implémente IList, IListSource, IBindingList ou IEnumerable.

Le paramètre dataMember doit être une propriété de la source de données qui retourne une collection pouvant être liée.Par exemple, une source DataSet a des tables comme données membres.

Exemples

L'exemple de code suivant crée un DataSet, un DataTable et un ListObject.Il lie ensuite ListObject à DataSet et à DataTable, mais inclut uniquement l'une des deux colonnes de table possibles dans ListObject.

Cet exemple illustre une personnalisation au niveau du document.

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

        ' Create a data table with two columns.
        Dim ds As New DataSet()
        Dim table As DataTable = ds.Tables.Add("Customers")
        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

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

        ' Bind the list object to the table.
        Dim mappedColumn As String() = {"Names"}
        List1.SetDataBinding(ds, "Customers", mappedColumn)

    End Sub

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

    // Create a data table with two columns.
    DataSet ds = new DataSet();
    DataTable table = ds.Tables.Add("Customers");
    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.
    string[] mappedColumn = { "Names" };
    list1.SetDataBinding(ds, "Customers", mappedColumn);
}

Sécurité .NET Framework

Voir aussi

Référence

ListObject Interface

SetDataBinding, surcharge

Microsoft.Office.Tools.Excel, espace de noms