Tuesday, April 9, 2013

Alternating Color on Repeating Section

You need to apply two conditions for formatting alternating color:

1.Expression :count(preceding-sibling::my:group14) mod 2 = 1   and i set  shading as "Yellow"--it has not worked.

2.Expression :count(preceding-sibling::my:group14) mod 2 = 0   and i set  shading as "Blue"--it has worked.
 when i use any one of those(1 or 2) its not working.

Only the combination(1 and 2) works

http://www.infopathdev.com/forums/p/20430/70754.aspx

Sunday, April 7, 2013

Retrieve a row of a repeating table

Problem: You have a repeating table on an InfoPath form and want to be able to retrieve the values of fields that are located in one specific row of the repeating table.

Solution:

You can retrieve rows of a repeating table either by their position within the repeating table or by the value of a specific field within the repeating table.

1 - Create a new InfoPath form

2  - Add a Repeating Table with 2 columns ("field1" & "field2")

3 - Add a Text Box control to the view and name it contents

4 - Add a second Text Box control to the view and name it toSelect

5 - Add a Button control and add the following code to the Clicked event handler:

A) If you want by position:


Public Sub CTRL14_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            If String.IsNullOrEmpty(toSelect) Then
                toSelect = "1"
            End If

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[" & toSelect & "]", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

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


        End Sub

B) If you want by field value:

        Public Sub CTRL15_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
            ' Write your code here.
            Dim mainDS As XPathNavigator = MainDataSource.CreateNavigator

            Dim toSelect As String = mainDS.SelectSingleNode("/my:myFields/my:toSelect", NamespaceManager).Value

            Dim row As XPathNavigator = mainDS.SelectSingleNode("/my:myFields/my:group1/my:group2[my:field1 = '" & toSelect & "']", NamespaceManager)

            Dim sb As New System.Text.StringBuilder()
            If row IsNot Nothing Then
                sb.Append(row.SelectSingleNode("my:field1", NamespaceManager).Value)
                sb.Append("; ")
                sb.Append(row.SelectSingleNode("my:field2", NamespaceManager).Value)
                sb.AppendLine()
            End If

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

        End Sub


6 - Save and preview the form

If the form retrieves rows by position, enter a number in the toSelect text box and click on the button.

If the form retrieves rows by field value then enter a piece of the text that is the same as the value of one of the field1 fields in any row of the repeating table.

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.

About Me

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