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.

ADVERTISEMENTS

Problem

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.

Solution

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.

Note:
A repeating node has only 2 events available:

  1. Changed event
  2. 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.

Discussion

You can accomplish this functionality as follows:

  1. Follow the instructions in Programmatically execute code when a repeating table row is inserted or deleted
  2. 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(
    "//my:group2", NamespaceManager);

    if (iter.Count > 3)
    e.Site.DeleteSelf();
    }

    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( _
    "//my:group2", NamespaceManager)

    If iter.Count > 3 Then
    e.Site.DeleteSelf()
    End If

    End If

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.

 
 Subscribe for updates via RSS or Email

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. 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, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services