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.

ADVERTISEMENTS

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:

  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 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 If

    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, the first row 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