How to get the rid of the first empty row in a repeating table in InfoPath

by S.Y.M. Wong-A-Ton

Repeating tables are always initialized with an empty first row, which you have to either remove or fill when programmatically adding rows to a repeating table. The XMLWriter object is used to write XML and not change existing XML, so filling the first row with data is not possible using this object.

There are two ways to solve this issue:

  1. If you have a loop that you use to populate the repeating table with rows, make sure you use the first item in the loop to fill the first row with data, and then continue looping through the other items and use the AppendChild method to create new rows in the repeating table. You can use code similar to the following code to populate the first row of a repeating table:
    XPathNavigator mainDOMNav = MainDataSource.CreateNavigator();
    XPathNavigator groupNav = mainDOMNav.SelectSingleNode("/my:myFields/my:group1/my:group2[1]",
    NamespaceManager);
    groupNav.SelectSingleNode("my:field1", NamespaceManager).SetValue("Cell 1");
    groupNav.SelectSingleNode("my:field2", NamespaceManager).SetValue("Cell 2");
    groupNav.SelectSingleNode("my:field3", NamespaceManager).SetValue("Cell 3");
  2. Loop through all your items, use AppendChild to create new rows in the repeating table, and then afterwards delete the first row by using code similar to the following code:
    // Delete first row
    XPathNavigator mainDOMNav = MainDataSource.CreateNavigator();
    XPathNavigator groupNav = mainDOMNav.SelectSingleNode("/my:myFields/my:group1/my:group2[1]",
    NamespaceManager);
    if (String.IsNullOrEmpty(groupNav.SelectSingleNode("my:field1", NamespaceManager).Value)
    && String.IsNullOrEmpty(groupNav.SelectSingleNode("my:field2", NamespaceManager).Value)
    && String.IsNullOrEmpty(groupNav.SelectSingleNode("my:field3", NamespaceManager).Value))
    {
      groupNav.DeleteSelf();
    }

Update (07 Jan 2009):

Since posting this, I’ve written more articles about deleting rows in repeating tables:

You might also be interested in:

 Subscribe (RSS | Email)

Related Posts

Comments to this post were closed 30 days after it was published.

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.

Working with InfoPath