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 源将表用作数据成员。

示例

下面的代码示例创建一个 DataSet、一个 DataTable 和一个 ListObject。然后将 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 命名空间