Powered By Blogger

Tuesday, January 17, 2012

Read and Bind InfoPath repeating table programmatically

Using methods of XPathNavigator class  repeating table of InfoPath can be bound with dataset / table / data view .  Values available in repeating table can also be accessed using methods of same class.

Line of code to bind and read repeating table in below data source,

 

 

To Bind : -

DataSet ds = new DataSet();

                XPathNavigator xNavigator = this.MainDataSource.CreateNavigator();

                ds = GetDatasetFromAdapter();

 

                XPathNavigator xRepeatingNavigator = xNavigator.SelectSingleNode("/my:myFields/my:Group/my:RepeatingGroup[1]", NamespaceManager);

                XPathNavigator xParentNavigator = xNavigator.SelectSingleNode("/my:myFields/my:Group", NamespaceManager);

 

                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)

                {

                    XPathNavigator node = xRepeatingNavigator.Clone();

                    node.SelectSingleNode("my:FirstValue", NamespaceManager).SetValue(ds.Tables[0].Rows[i]["FirstValue"].ToString());

                    node.SelectSingleNode("my:SecondValue", NamespaceManager).SetValue(ds.Tables[0].Rows[i]["SecondValue"].ToString());

                    xParentNavigator.AppendChild(node);

 

                }

                xRepeatingNavigator.DeleteSelf();

 

To Read : -

XPathNavigator xNavigator = this.MainDataSource.CreateNavigator();

                XPathNodeIterator xNodeIterator = xNavigator.Select("/my:myFields/my:Group/my:RepeatingGroup", NamespaceManager);

                string FirstValue = string.Empty;

                string SecondValue = string.Empty;

                foreach (XPathNavigator Node in xNodeIterator)

                {

                    FirstValue = Node.SelectSingleNode("my:FirstValue", NamespaceManager).Value;

                    SecondValue = Node.SelectSingleNode("my:SecondValue", NamespaceManager).Value;

                }

2 comments:

  1. Great Post,
    For to read and bind data in re
    Could you explain How to use
    ds = GetDatasetFromAdapter();

    Balaji

    ReplyDelete
    Replies
    1. Just use ado.net methods to get the dataset.

      Delete