Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Excel Host Controls
 How to: Fill ListObject Controls wi...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Tools for the Microsoft Office System
How to: Fill ListObject Controls with Data

Note Required applications

The features in this topic are available only if you have the required applications installed.

For more information, see Features Available by Product Combination.

  • One of these development environments:

    VSTO 2005

    -or-

    Visual Studio Team System

  • Microsoft Office 2003

You can use data binding as a way to quickly add data to your document. After binding data to a list object, you can call the Disconnect method of the ListObject so that the ListObject displays the data but is no longer bound to the data source.

To bind data to a ListObject control

  1. Create a DataTable at the class level.

    Visual Basic
    Dim table As DataTable = New DataTable("Employees")
    
    C#
    DataTable table = new DataTable("Employees");
    
  2. Add sample columns and data in the Startup event handler of Sheet1.

    Visual Basic
    Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Me.Startup
    
        table.Columns.Add("FirstName", GetType(String))
        table.Columns.Add("LastName", GetType(String))
        table.Columns.Add("Title", GetType(String))
    
        table.Rows.Add("Nancy", "Anderson", "Sales Representative")
        table.Rows.Add("Robert", "Brown", "Sales Representative")
    
    C#
    private void Sheet1_Startup(object sender, System.EventArgs e)
    {
        table.Columns.Add("FirstName", typeof(string));
        table.Columns.Add("LastName", typeof(string));
        table.Columns.Add("Title", typeof(string));
    
        table.Rows.Add("Nancy", "Anderson", "Sales Representative");
        table.Rows.Add("Robert", "Brown", "Sales Representative");
    
  3. Call the SetDataBinding method and pass in the column names in the order they should appear. The order of the columns in the list object can differ from the order in which they appear in the DataTable.

    Visual Basic
    Dim List1 As Microsoft.Office.Tools.Excel.ListObject = _
        Me.Controls.AddListObject(Me.Range("A1", "B3"), "List1")
    
    List1.AutoSetDataBoundColumnHeaders = True
    List1.SetDataBinding(table, "Title", "LastName", "FirstName")
    
    C#
    Microsoft.Office.Tools.Excel.ListObject List1 = 
        this.Controls.AddListObject(this.Range["A1", "B3"], "List1");
    
    List1.AutoSetDataBoundColumnHeaders = true;
    List1.SetDataBinding(table, "Title", "LastName", "FirstName");
    

To disconnect the ListObject control from the data source

  • Call the Disconnect method of List1.

    Visual Basic
        List1.Disconnect()
    End Sub
    
    C#
        List1.Disconnect();
    }
    

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker