Programmatically prevent users from adding more rows to a repeating table in InfoPath
Learn how you can use the Changed event of a repeating node and the DeleteSelf() method to programmatically prevent a user from adding more rows to a repeating table in InfoPath when the repeating table already contains a certain amount of rows.
You have a Repeating Table on an InfoPath form and when the Repeating Table already contains 3 rows, you want to prevent users from adding more rows to the it.
Use the Changed event of the repeating group node of the Repeating Table and the DeleteSelf() method of the Site object to programmatically prevent more rows from being added to a Repeating Table control on an InfoPath form.
A repeating node has only 2 events available:
- Changed event
- Validating event
The MSDN documentation says that neither the Changed event nor the Validating event allows users to cancel an operation, so you will have to find workarounds (as described in this article) if you want to cancel or undo operations performed on the rows or sections of a repeating node, that is, when inserting or deleting rows or sections.
You can accomplish this functionality as follows:
- Follow the instructions in Programmatically execute code when a repeating table row is inserted or deleted
Add the following C# code to the Changed event handler of the group2 node:
if (e.Operation == XmlOperation.Insert)
// Get a reference to the Main data source
XPathNavigator domNav = MainDataSource.CreateNavigator();
// Count the amount of rows (group2 nodes). If there are
// more than 3 rows, delete the current row, which is the
// last row that was added
XPathNodeIterator iter = domNav.Select(
if (iter.Count > 3)
Or add the following Visual Basic code to the Changed event handler of the group2 node:
If e.Operation = XmlOperation.Insert Then
' Get a reference to the Main data source
Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()
' Count the amount of rows (group2 nodes). If there are
' more than 3 rows, delete the current row, which is the
' last row that was added
Dim iter As XPathNodeIterator = domNav.Select( _
If iter.Count > 3 Then
You should now have a fully functional form so that if you have already added 3 rows to the repeating table, you will not be able to insert another row to the repeating table.
Related InfoPath Articles:
- Programmatically execute code when a repeating table row is inserted or deleted
- 4 Ways to programmatically add a row to a repeating table in InfoPath
- Count the total amount of sections in a Repeating Section in InfoPath on insert or delete
- Programmatically delete all of the rows of a repeating table in InfoPath
- Repeating control basics in InfoPath
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. The 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.