Programmatically delete the first row of a repeating table in InfoPath
Learn how to use C# or Visual Basic code to programmatically delete the first row of a repeating table in InfoPath.
Problem
You have a repeating table in which the first row contains empty values, so you want to programmatically delete this row.
Solution
Use the DeleteSelf() method of the XPathNavigator object to delete the first row 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 the first row of the repeating table
XPathNavigator domNav = MainDataSource.CreateNavigator();
XPathNavigator itemNav = domNav.SelectSingleNode(
"/my:myFields/my:group1/my:group2[1]",
NamespaceManager);
// Delete the row
if (itemNav != null)
itemNav.DeleteSelf();
Or add the following Visual Basic code to the Clicked event:
' Retrieve the first row of the repeating table
Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()
Dim itemNav As XPathNavigator = domNav.SelectSingleNode( _
"/my:myFields/my:group1/my:group2[1]", _
NamespaceManager)
' Delete the row
If itemNav IsNot Nothing Then
itemNav.DeleteSelf()
End IfNote: 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, the first row of the repeating table will be deleted.
Related InfoPath Articles:
- How to loop through items in a repeating table in InfoPath 2007
- Programmatically delete all of the rows 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.



