Calculation on Repeating Table Changed Event in InfoPath


We had a requirement of creating an InfoPath form that would use a repeating table and to do certain calculations on the values in rows of the repeating tables and display the result in a separate section.

Here is a screen shot of the same !

infopathCalculation

So above here, based on activity selected and time spent specified for it, the Total hours and Total days ( total hours/ 8) needs to be calculated.

Here is the sample code for that

public void group7_Changed(object sender, XmlEventArgs e)
       {
           // Create an XPathNavigator from the main data source
           XPathNavigator domNav = MainDataSource.CreateNavigator();
           // Create an XPathNodeIterator to iterate through all the rows
           XPathNodeIterator rows = domNav.Select(
           "/my:myFields/my:group6/my:group7", NamespaceManager);
           // Create Navigator for each of the fields where total needs to be displayed
           XPathNavigator lblTotalSpecHours = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblSpecHours", NamespaceManager);
           Int32 intTotalSpecHours = 0;
           XPathNavigator lblTotalConsultancyHours = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalConsulHours", NamespaceManager);
           Int32 intTotalConsultancyHours = 0;
           XPathNavigator lblTotalDevelopmentHours = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalDevHours", NamespaceManager);
           Int32 intTotalDevelopmentHours = 0;
           XPathNavigator lblTotalTestingHours = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalTestingHours", NamespaceManager);
           Int32 intTotalTestingHours = 0;
           XPathNavigator lblTotalDeploymentHours = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalDepHours", NamespaceManager);
           Int32 intTotalDeploymentHours = 0;

           // loop through all the rows of repeating table
           while (rows.MoveNext())
           {
               // Get values for Activity dropdown field and Time Spent field
               string fldActivity = rows.Current.SelectSingleNode(
               "my:ddlActivity", NamespaceManager).Value;
               string fldTimeSpent = rows.Current.SelectSingleNode(
               "my:txtTimeSpent", NamespaceManager).Value;                 

               if (fldActivity == "Specification" && fldTimeSpent!="")
               {
                   intTotalSpecHours += Convert.ToInt32(fldTimeSpent);                       
               }

               if (fldActivity == "Consultancy" && fldTimeSpent != "")
               {
                   intTotalConsultancyHours += Convert.ToInt32(fldTimeSpent);
               }

               if (fldActivity == "Development" && fldTimeSpent != "")
               {
                   intTotalDevelopmentHours += Convert.ToInt32(fldTimeSpent);
               }

               if (fldActivity == "Testing" && fldTimeSpent != "")
               {
                   intTotalTestingHours += Convert.ToInt32(fldTimeSpent);
               }
               if (fldActivity == "Deployment" && fldTimeSpent != "")
               {
                   intTotalDeploymentHours += Convert.ToInt32(fldTimeSpent);
               }          

           }

           // for Specification
           RemoveNilAndSetHours(lblTotalSpecHours, intTotalSpecHours);
           XPathNavigator lblTotalSpecDays = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblSpecDays", NamespaceManager);
           RemoveNilAndSetDays(lblTotalSpecDays, intTotalSpecHours);       

           // for Consultancy
           RemoveNilAndSetHours(lblTotalConsultancyHours, intTotalConsultancyHours);
           XPathNavigator lblTotalConsultancyDays = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalConsulDays", NamespaceManager);
           RemoveNilAndSetDays(lblTotalConsultancyDays, intTotalConsultancyHours);

           //// for Development
           RemoveNilAndSetHours(lblTotalDevelopmentHours, intTotalDevelopmentHours);
           XPathNavigator lblTotalDevelopmentDays = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalDevDays", NamespaceManager);
           RemoveNilAndSetDays(lblTotalDevelopmentDays, intTotalDevelopmentHours);

           //// for Testing
           RemoveNilAndSetHours(lblTotalTestingHours, intTotalTestingHours);
           XPathNavigator lblTotalTestingDays = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalTestingDays", NamespaceManager);
           RemoveNilAndSetDays(lblTotalTestingDays, intTotalTestingHours);

           //// for Deployment
           RemoveNilAndSetHours(lblTotalDeploymentHours, intTotalDeploymentHours);
           XPathNavigator lblTotalDeploymentDays = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:lblTotalDepDays", NamespaceManager);
           RemoveNilAndSetDays(lblTotalDeploymentDays, intTotalDeploymentHours); 
       }

       // RemoveNil for the fields and set value
       public void RemoveNilAndSetDays(XPathNavigator xpathNav, int totalHours)
       {
           if (xpathNav.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
               xpathNav.DeleteSelf();
           int days = totalHours / 8;
           xpathNav.SetValue(days.ToString());
       }

       public void RemoveNilAndSetHours(XPathNavigator xpathNav, int totalHours)
       {
           if (xpathNav.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
               xpathNav.DeleteSelf();
           xpathNav.SetValue(totalHours.ToString());
       }

 

 

Change the extension of the file from .doc to .xsn !!!

Bye..


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.