Programmatically delete all of the rows of a repeating table in InfoPath
Learn how to use C# or Visual Basic code to programmatically delete all of the rows of a repeating table in InfoPath.
Problem
You have a repeating table containing rows of data and you want to delete all of these rows.
Solution
Use the DeleteRange() method of the XPathNavigator object to delete all of the rows in a repeating table.
Discussion
You can achieve this functionality as follows:
- Create a new Blank InfoPath form template, and add a Repeating Table and a Button control to it.
Figure 1. The the form template in Design mode.
The Main data source is shown in the following figure:
Figure 2. The Main data source of the form template.
- Double-click the Button control to open its Properties dialog box.
- On the Button Properties dialog box, click Edit Form Code to add a Clicked event handler for the button.
-
Add the following C# code to the Clicked event:
// Retrieve a reference to the Main DOM
XPathNavigator root = MainDataSource.CreateNavigator();
// Count the amount of group2 nodes
XPathNodeIterator iter = root.Select(
"/my:myFields/my:group1/my:group2",
NamespaceManager);
int group2NodesCount = iter.Count;
// Retrieve the first group2 node
XPathNavigator firstGroup2NodeNav =
root.SelectSingleNode("/my:myFields/my:group1/my:group2[1]",
NamespaceManager);
// Retrieve the last group2 node
XPathNavigator lastGroup2NodeNav =
root.SelectSingleNode("/my:myFields/my:group1/my:group2[" +
group2NodesCount.ToString() + "]",
NamespaceManager);
// Delete the range of nodes from the first to the last group2 nodes
firstGroup2NodeNav.DeleteRange(lastGroup2NodeNav);
Or add the following Visual Basic code to the Clicked event:
' Retrieve a reference to the Main DOM
Dim root As XPathNavigator = MainDataSource.CreateNavigator()
' Count the amount of group2 nodes
Dim iter As XPathNodeIterator = root.Select( _
"/my:myFields/my:group1/my:group2", _
NamespaceManager)
Dim group2NodesCount As Integer = iter.Count
' Retrieve the first group2 node
Dim firstGroup2NodeNav As XPathNavigator = _
root.SelectSingleNode("/my:myFields/my:group1/my:group2[1]", _
NamespaceManager)
' Retrieve the last group2 node
Dim lastGroup2NodeNav As XPathNavigator = _
root.SelectSingleNode("/my:myFields/my:group1/my:group2[" & _
group2NodesCount.ToString() & "]", _
NamespaceManager)
' Delete the range of nodes from the first to the last group2 nodes
firstGroup2NodeNav.DeleteRange(lastGroup2NodeNav)
Note: You can use the Copy XPath functionality in the Data Source pane (also see Programmatically retrieve the value of an InfoPath form field using .NET code) to find out what the XPath expression of the group2 node should be. Then you can add the XPath expression filter [1] to retrieve the first row of the repeating table.
- Save your work, build the code, and test the form.
Now when you click the button, all of the rows of the repeating table will be deleted.
Related InfoPath Articles:
- How to loop through items in a repeating table in InfoPath 2007
- Programmatically delete the first row of a repeating table in InfoPath
- Programmatically retrieve the value of an InfoPath form field using .NET code
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 of S.Y.M. Wong-A-Ton. Usage of 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, S.Y.M. Wong-A-Ton.



