Programmatically hide the first row of a repeating table in InfoPath
Learn how you can use a hidden field and conditional formatting to programmatically hide the first row of a repeating table in InfoPath.
Problem
You have a repeating table on an InfoPath form, which you want to add rows to and then click a button to hide the first row of the repeating table.
Solution
Use conditional formatting and a hidden field to programmatically hide the first row of a repeating table.
Discussion
You can achieve this functionality as follows:
- Create a new Blank InfoPath form template.
- Add a Repeating Table and Button control to the form template.
- On the Data Source task pane, right-click the group2 node of the repeating table and select Add from the drop-down menu that appears.
- On the Add Field or Group dialog box, type hidden in the Name text box
and click OK. The Main data source of the InfoPath form template should now resemble the following
figure.
Figure 1. The Main data source of the InfoPath form template. - On the InfoPath form template, select the repeating table, and then from the Format menu, choose Conditional Formatting.
- On the Conditional Formatting dialog box, click Add.
On the Conditional Format dialog box, set up a condition that says:
hidden is equal to "1"- On the Conditional Format dialog box, select the Hide this control check box, and click OK.
- On the Conditional Formatting dialog box, click OK.
- Double-click the button control to open its Properties dialog box.
- On the Button Properties dialog box, click Edit Form Code.
Add the following C# code to the Clicked event handler of the button control:
XPathNavigator root = MainDataSource.CreateNavigator();
root.SelectSingleNode(
"//my:hidden[1]", NamespaceManager).SetValue("1");
- Save your work and build the code for the InfoPath form template.
Now whenever you fill out the InfoPath form, add a few rows to the repeating table and click on the button, the first row in the repeating table will be hidden.
Related InfoPath Articles:
