ListObject.SetDataBinding Method (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.

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

void SetDataBinding(
	object dataSource,
	string dataMember,
	params string[] mappedColumns
)

Parameters

dataSource
Type: System.Object

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

dataMember
Type: System.String

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

mappedColumns
Type: System.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.

Exception Condition
SetDataBindingFailedException

Could not bind to the specified data source.

ArgumentException

One or more of the arguments are invalid.

ArgumentNullException

The dataSource argument is null.

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.

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);
}
Return to top
Show: