共用方式為


ListObject.SetDataBinding 方法 (Object, String, array<String[])

ListObject 控制項繫結至資料來源中指定的資料成員,並只顯示此資料成員中指定的欄。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)

語法

'宣告
Sub SetDataBinding ( _
    dataSource As Object, _
    dataMember As String, _
    ParamArray mappedColumns As String() _
)
void SetDataBinding(
    Object dataSource,
    string dataMember,
    params string[] mappedColumns
)

參數

  • mappedColumns
    型別:array<System.String[]
    您要在 ListObject 控制項中顯示之資料成員的欄名稱。若要加入未對應的資料行,請在此陣列中包含一個空字串。

例外狀況

例外狀況 條件
SetDataBindingFailedException

無法繫結至指定的資料來源。

ArgumentException

一或多個引數無效。

ArgumentNullException

dataSource 引數為 nullnull 參考 (即 Visual Basic 中的 Nothing)。

備註

dataSource 參數可以是實作 IListIListSourceIBindingListIEnumerable 的任何物件。

dataMember 參數必須是資料來源的屬性,而這個資料來源會傳回可繫結的集合。 例如資料成員是資料表的 DataSet 來源。

範例

下列程式碼範例會建立 DataSetDataTableListObject。 範例接著將 ListObject 繫結至 DataSetDataTable,但是只會在 ListObject 中包含其中一個資料表資料行。

這是示範文件層級自訂的範例。

    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);
}

.NET Framework 安全性

請參閱

參考

ListObject 介面

SetDataBinding 多載

Microsoft.Office.Tools.Excel 命名空間