Skip to content

Latest commit

 

History

History
79 lines (61 loc) · 4.04 KB

how-to-fill-listobject-controls-with-data.md

File metadata and controls

79 lines (61 loc) · 4.04 KB
title description ms.date ms.topic dev_langs helpviewer_keywords author ms.author manager ms.subservice
Fill ListObject controls with data
Use data binding to quickly add data to your document. You can also disconnect the list object so it displays the data but is no longer bound to the data source.
02/02/2017
how-to
VB
CSharp
disconnecting from data sources
ListObject control, disconnecting from a data source
ListObject control, filling with data
data [Office development in Visual Studio], adding to worksheets
data binding, ListObject controls
worksheets, populating with data
John-Hart
johnhart
mijacobs
office-development

Fill ListObject controls with data

You can use data binding as a way to quickly add data to your document. After binding data to a list object, you can disconnect the list object so it displays the data but is no longer bound to the data source.

[!INCLUDEappliesto_xlalldocapp]

To bind data to a ListObject control

  1. Create a xref:System.Data.DataTable at the class level.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreHostControlsExcelCS/Sheet4.cs" id="Snippet20":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_VstcoreHostControlsExcelVB/Sheet4.vb" id="Snippet20":::

  2. Add sample columns and data in the Startup event handler of the Sheet1 class (in a document-level project) or ThisAddIn class (in an application-level project).

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreHostControlsExcelCS/Sheet4.cs" id="Snippet21":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_VstcoreHostControlsExcelVB/Sheet4.vb" id="Snippet21":::

  3. Call the xref:Microsoft.Office.Tools.Excel.ListObject.SetDataBinding%2A 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 xref:System.Data.DataTable.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreHostControlsExcelCS/Sheet4.cs" id="Snippet22":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_VstcoreHostControlsExcelVB/Sheet4.vb" id="Snippet22":::

To disconnect the ListObject control from the data source

  1. Call the xref:Microsoft.Office.Tools.Excel.ListObject.Disconnect%2A method of List1.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreHostControlsExcelCS/Sheet4.cs" id="Snippet23":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_VstcoreHostControlsExcelVB/Sheet4.vb" id="Snippet23":::

Compile the code

This code example assumes you have an existing xref:Microsoft.Office.Tools.Excel.ListObject named list1 on the worksheet in which this code appears.

Related content