Programmatically add a row to a repeating table using an XmlWriter object

Use the XmlWriter object that is returned by the AppendChild method of the XPathNavigator object to add a row to a repeating table.

ADVERTISEMENTS

Scenario

You want to programmatically add a row to a repeating table by clicking a button.

Solution

Use the XmlWriter object that is returned by the AppendChild method of the XPathNavigator object to add a row to a repeating table.

Discussion

The AppendChild method has 4 overloads, one of which returns an XmlWriter object that you can use to create a row in a repeating table.

Before you can get started with writing code for InfoPath form templates, you have to have your programming environment correctly set up and be able to write C# or Visual Basic .NET code. See the following articles for information on what you must have installed before beginning to write code:

Once you've got your programming environment set up, you can accomplish the functionality as follows:

  1. Design an InfoPath form template as shown in figure 1.

    Figure 1. The InfoPath form template in design mode.

    with a Main data source that resembles the following figure:


    Figure 2. The Main data source of the form template.
  2. Double-click the Add Row button 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 to the form template.
  4. Add the following code to the Clicked event handler of the button:
    C#
    string myNamespace = NamespaceManager.LookupNamespace("my");
    using (XmlWriter writer = MainDataSource.CreateNavigator().SelectSingleNode(
    "/my:myFields/my:group1", NamespaceManager).AppendChild())
    {
    writer.WriteStartElement("group2", myNamespace);
    writer.WriteElementString("field1", myNamespace, "Cell 1");
    writer.WriteElementString("field2", myNamespace, "Cell 2");
    writer.WriteElementString("field3", myNamespace, "Cell 3");
    writer.WriteEndElement();
    writer.Close();
    }

    Visual Basic
    Dim myNamespace As String = NamespaceManager.LookupNamespace("my")
    Using writer As XmlWriter = MainDataSource.CreateNavigator().SelectSingleNode( _
    "/my:myFields/my:group1", NamespaceManager).AppendChild()
    writer.WriteStartElement("group2", myNamespace)
    writer.WriteElementString("field1", myNamespace, "Cell 1")
    writer.WriteElementString("field2", myNamespace, "Cell 2")
    writer.WriteElementString("field3", myNamespace, "Cell 3")
    writer.WriteEndElement()
    writer.Close()
    End Using
  5. Save, build, and test your work.
 
 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