Partager via


ListObject.SetDataBinding Method

Definition

Overloads

SetDataBinding(Object, String, String[])

Binds a ListObject control to a specified data member of a data source, and shows only specified columns of that data member.

SetDataBinding(Object, String)

Binds a ListObject control to a specified data member of a data source.

SetDataBinding(Object)

Binds a ListObject control to a data source.

SetDataBinding(Object, String, String[])

Binds a ListObject control to a specified data member of a data source, and shows only specified columns of that data member.

public:
 void SetDataBinding(System::Object ^ dataSource, System::String ^ dataMember, ... cli::array <System::String ^> ^ mappedColumns);
public void SetDataBinding (object dataSource, string dataMember, params string[] mappedColumns);
abstract member SetDataBinding : obj * string * string[] -> unit
Public Sub SetDataBinding (dataSource As Object, dataMember As String, ParamArray mappedColumns As String())

Parameters

dataSource
Object

The object to use as a data source for the ListObject control.

dataMember
String

The DataMember that specifies the property to bind to within the object returned by the DataSource.

mappedColumns
String[]

Names of columns in the data member that you want to display in the ListObject control. To add an unmapped column, include an empty string in the array.

Exceptions

Could not bind to the specified data source.

One or more of the arguments are invalid.

The dataSource argument is null.

Examples

The following code example creates a DataSet, a DataTable, and a ListObject. It then binds the ListObject to the DataSet and the DataTable, but only includes one of the two possible table columns in the ListObject.

This example is for a document-level customization.

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

Remarks

The dataSource parameter can be any object that implements IList, IListSource, IBindingList, or IEnumerable.

The dataMember parameter must be a property of the data source that returns a bindable collection. For example, a DataSet source has tables as data members.

Applies to

SetDataBinding(Object, String)

Binds a ListObject control to a specified data member of a data source.

public:
 void SetDataBinding(System::Object ^ dataSource, System::String ^ dataMember);
public void SetDataBinding (object dataSource, string dataMember);
abstract member SetDataBinding : obj * string -> unit
Public Sub SetDataBinding (dataSource As Object, dataMember As String)

Parameters

dataSource
Object

The object to use as a data source for the ListObject control.

dataMember
String

The DataMember that specifies the property to bind to within the object returned by the DataSource.

Exceptions

Could not bind to the specified data source.

One or more of the arguments are invalid.

The dataSource argument is null.

Examples

The following code example creates a DataSet, a DataTable, and a ListObject. It then binds the list object to the DataSet and the DataTable.

This example is for a document-level customization.

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

Remarks

The data source can be any object that implements IList, IListSource, IBindingList, or IEnumerable.

The data member must be a property of the data source that returns a bindable collection. For example, a DataSet source has tables as data members.

Applies to

SetDataBinding(Object)

Binds a ListObject control to a data source.

public:
 void SetDataBinding(System::Object ^ dataSource);
public void SetDataBinding (object dataSource);
abstract member SetDataBinding : obj -> unit
Public Sub SetDataBinding (dataSource As Object)

Parameters

dataSource
Object

The object to use as a data source for the ListObject control.

Exceptions

Could not bind to the specified data source.

The argument is invalid.

The dataSource argument is null.

Examples

The following code example demonstrates how to use the SetDataBinding method to bind a ListObject to a DataTable. The DataTable contains two columns, which contain the names and ages of employees, and four rows that represent employee entries.

This example is for a document-level customization.

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

Remarks

The data source can be any object that implements IList, IListSource, IBindingList, or IEnumerable, such as a DataTable or a one-dimensional array.

Applies to