ListObject.SetDataBinding Method (Object)
Visual Studio 2012
Binds a ListObject control to a data source.
Assembly: Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)
Parameters
- dataSource
- Type: Object
The object to use as a data source for the ListObject control.
| Exception | Condition |
|---|---|
| SetDataBindingFailedException | Could not bind to the specified data source. |
| ArgumentException | The argument is invalid. |
| ArgumentNullException | The dataSource argument is null. |
The data source can be any object that implements IList, IListSource, IBindingList, or IEnumerable, such as a DataTable or a one-dimensional array.
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); }
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.