How to loop through items in a repeating table in InfoPath 2007

Applies to: InfoPath 2007

Use the XPathNodeIterator class with C# or VB.NET code to loop through the rows and items in a repeating table.

ADVERTISEMENTS

Problem

You have a repeating table for which you would like to programmatically loop through all of its items and retrieve the value of each field in each row.

Solution

Use the XPathNodeIterator class in C# or Visual Basic code to loop through the rows and items in a repeating table.

Discussion

You can accomplish this functionality as follows:

  1. Open Microsoft Office InfoPath 2007, create a new blank form, and add a Repeating Table and a Button control to the form template.
  2. In the Data Source pane, rename group1 to table and group2 to row.
    The main data source of your form template should resemble the following figure:


    Figure 1. The main data source of the form template in Design mode.
  3. Double-click the Button control to open its Properties dialog box.
  4. On the Button Properties dialog box, click Edit Form Code.
  5. Add the following code to the Clicked event handler of the Button control:

    C# XPathNavigator domNav = MainDataSource.CreateNavigator();
    XPathNodeIterator rows = domNav.Select(
    "/my:myFields/my:table/my:row", NamespaceManager);

    while (rows.MoveNext())
    {
    string field1 = rows.Current.SelectSingleNode(
    "my:field1", NamespaceManager).Value;
    string field2 = rows.Current.SelectSingleNode(
    "my:field2", NamespaceManager).Value;
    string field3 = rows.Current.SelectSingleNode(
    "my:field3", NamespaceManager).Value;
    }


    Visual Basic Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()
    Dim rows As XPathNodeIterator = domNav.Select( _
    "/my:myFields/my:table/my:row", NamespaceManager)

    While rows.MoveNext()
    Dim field1 As String = rows.Current.SelectSingleNode( _
    "my:field1", NamespaceManager).Value
    Dim field2 As String = rows.Current.SelectSingleNode( _
    "my:field2", NamespaceManager).Value
    Dim field3 As String = rows.Current.SelectSingleNode( _
    "my:field3", NamespaceManager).Value
    End While
  6. Save your work, build the project, and test the form template.
 


Related InfoPath Articles:

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services