Método ListObject.SetDataBinding (Object, String)

Vincula um ListObject o controle a um membro de dados especificado de uma fonte de dados.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (em Microsoft.Office.Tools.Excel.dll)

Sintaxe

'Declaração
Sub SetDataBinding ( _
    dataSource As Object, _
    dataMember As String _
)
void SetDataBinding(
    Object dataSource,
    string dataMember
)

Parâmetros

Exceções

Exceção Condição
SetDataBindingFailedException

Não pôde ligar-se à fonte de dados especificado.

ArgumentException

Um ou mais argumentos são inválidos.

ArgumentNullException

O dataSource argumento é nulluma referência nula (Nothing no Visual Basic).

Comentários

A fonte de dados pode ser qualquer objeto que implementa IList, IListSource, IBindingList, ou IEnumerable.

O membro de dados deve ser uma propriedade da fonte de dados que retorna uma coleção ligável. Por exemplo, um DataSet fonte possui tabelas como membros de dados.

Exemplos

O exemplo de código a seguir cria um DataSet, um DataTablee um ListObject. Ele então liga o objeto de lista para o DataSet e o DataTable.

Este exemplo é para uma personalização em nível de documento.

    Private Sub ListObject_SetDataBinding2()
        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.
        List1.SetDataBinding(ds, "Customers")

    End Sub

private void ListObject_SetDataBinding2()
{
    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.
    list1.SetDataBinding(ds, "Customers");
}

Segurança do .NET Framework

Consulte também

Referência

ListObject Interface

Sobrecargas SetDataBinding

Namespace Microsoft.Office.Tools.Excel