Wednesday, April 11, 2012

InfoPath 2010 Repeating Table Updates Field On Change

When working in InfoPath you can run into business situations where a fields value is set based on user selections in other fields.  This can add a layer of complexity when the fields exist in a Repeating Table as each row needs to be executed independently.  But do nt worry there is a simple solution for this requirement as each InfoPath form is just a structured XML document on the back end.  This solution is given as code behind as this provides the quickest solution.  The sample below executes on a change event of a control in a repeating table and not on the insertion of a row as this would present null issues in the code.

public void Control_Changed(object sender, XmlEventArgs e)

{
//check to ensure value changed and not just row add if so run the code
if (e.Operation == XmlOperation.ValueChange)
{
//connect to current row in dom
XPathNavigator xnDoc = e.Site.CreateNavigator();
xnDoc.MoveToParent();                               
//select node that was changed in current row
string myVar = xnDoc.SelectSingleNode("my:Node", this.NamespaceManager).Value.ToString();
//select node in current row to update
XPathNavigator UpdateNode = xnDoc.SelectSingleNode("my:NodeToUpdate"this.NamespaceManager);
UpdateNode.SetValue(myVar);
}

}

3 comments:

Anonymous said...

Thanks man, I ve been looking for this all day

Anonymous said...

Thanks man, Ive been looking for this all day

Nico said...

Excellent article! thank you!