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.

ADVERTISEMENTS

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:

  1. Create a new Blank InfoPath form template, and add a Repeating Table and a Button control to it.

    The the form template in Design mode
    Figure 1. The the form template in Design mode.

    The Main data source is shown in the following figure:

    The Main data source of the form template
    Figure 2. The Main data source of the form template.

  2. Double-click the Button control to open its Properties dialog box.
  3. On the Button Properties dialog box, click Edit Form Code to add a Clicked event handler for the button.
  4. 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.

  5. 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.

 
 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