Sunday, April 7, 2013

Loop through rows of a repeating table

Problem: You have a repeating table on an InfoPath form and want to sequentially retrieve each row of the repeating table

Solution:
You can use the XPathNodeIterator object to loop through rows of a repeating table.

1 - Create a new InfoPath form

2 - Add a Repeating Table control with 2 columns

3 - Add a Text Box control to the view of the form and name it contents. Select it's Multi-Line property.

4 - Add a Button control to the form and on it's Clicked Event Handler write:


Public Sub CTRL8_11_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.

            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim rows As XPathNodeIterator = mainDS.Select("/my:myFields/my:group3/my:group4", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()

            While rows.MoveNext

                sb.Append(rows.Current.SelectSingleNode("my:field4", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(rows.Current.SelectSingleNode("my:field5", NamespaceManager).Value)
                sb.AppendLine()
            End While

            mainDS.SelectSingleNode("/my:myFields/my:contents", NamespaceManager).SetValue(sb.ToString())

        End Sub


5 - Save and preview the form

When the form opens, add a couple of rows of data to the repeating table and then click on the button. The Text Box will display all data you entered into the fields in the Repeating table separated by semi-colons.

1 comment:

  1. infopath signing
    Thank you so much for the easy explanation about how to solve the problem of repeating table. Several days I was trying to understand what should I do, your post really helped me. Thank you.

    ReplyDelete

About Me

My photo
Toronto, Ontario, Canada
MBA, M.Sc. & MCP