Get Value from currently selected node - InfoPath Dev
in

InfoPath Dev

Get Value from currently selected node

Last post 01-05-2009 08:30 AM by Patrick. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-02-2009 11:34 AM

    • Patrick
    • Top 500 Contributor
      Male
    • Joined on 11-04-2007
    • Texas
    • Posts 51

    Get Value from currently selected node

    I hope this is a quick and easy one...

    I'm using InfoPath 2007 and C# code. 

    I have a master/detail relationship and I want a value from the "Master" table to populate in a field that is not part of the repeating group that the master/detail is a part of.  I am trying to get a value from a single field in the repeating table to the destination field.  The value would change whenever the user selects a new row.  The destination field is just a text box (non-repeating).

    So basically I want to take a value from a field in the currently selected row of a repeating table and copy it to a non-repeating field, and have it change if the user selects a different row.  How do I reference the currently selected row in a table?

     
    Thanks!
     

    -Patrick
    Filed under: , , ,
  • 01-02-2009 07:06 PM In reply to

    Re: Get Value from currently selected node

    Set the default value of the destination field to the following XPath formula (making the appropriate modifications), but don't check "Update this value when the result of the formula is recalculated."  This will set that destination node to the appropriate value when the form loads.

    /my:myFields/my:group1/my:group2[position() = xdXDocument:GetNamedNodeProperty(/my:myFields/my:group1, "CTRL1_5", 1)]/my:field2

    Here

    • /my:myFields/my:group1/my:group2 is the repeating node
    • /my:myFields/my:group1 is the node that contains the repeating node
    • CTRL1_5 is the value of the ViewContext property of the repeating table (you can find this value by viewing the repeating table's properties and looking at the Advanced tab)
    • my:field2 is the child of my:group2 that you want to copy to the destination field

    Then adding a Context Change event to your form with code like the following should take care of the rest:

    public void FormEvents_ContextChanged(object sender, ContextChangedEventArgs e)
    {
         if (e.ChangeType == "ContextNode")
         {
              XPathNavigator repeatingNode = e.Context.SelectSingleNode("ancestor-or-self::my:group2", NamespaceManager);
              if (repeatingNode != null)
              {
                   XPathNavigator destinationNode = repeatingNode.SelectSingleNode("/my:myFields/my:outsideField", NamespaceManager);
                   destinationNode.SetValue(repeatingNode.SelectSingleNode(
    "my:field2", NamespaceManager).Value);
              }
              return;
         }
    }

    Again, my:group2 is the repeating node, /my:myFields/my:outsideField is the destination node, and my:field2 is the field whose value I want to copy.

    That should work for most situations.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 01-05-2009 08:30 AM In reply to

    • Patrick
    • Top 500 Contributor
      Male
    • Joined on 11-04-2007
    • Texas
    • Posts 51

    Re: Get Value from currently selected node

    Thanks Jimmy!  Worked great :)

    -Patrick
Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.