How to loop through items in a repeating table in InfoPath 2007
Use the XPathNodeIterator class with C# or VB.NET code to loop through the rows and items in a repeating table.
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:
- Open Microsoft Office InfoPath 2007, create a new blank form, and add a Repeating Table and a Button control to the form template.
- 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. - Double-click the Button control to open its Properties dialog box.
- On the Button Properties dialog box, click Edit Form Code.
- 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 BasicDim 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 - Save your work, build the project, and test the form template.
Related InfoPath Articles:
