Translate

Thursday 24 January 2013

“Decimal not displaying in SUM” Issue in DevExpress XtraReport.

Hi.. I AM back again..  :).. This time with a concern which i faced at my work place while working on DevXpress XtraReport tool.

XtraReport is a tool used for generating reports. The Issue was: When i apply the SUM function of XtraReport on Some column data then i was facing concerns of my trailing zeroes getting compressed and for a finance domain client: Rs25,000.00 looks better than Rs25,000.. He He..

The Summary Functions provided by XtraReport includes: Aggregate,Sum,DCount(Distinct Count) etc..

For Eg: I have applied a SUM function on a label , bound to print Sum of a Column that has No.Of Shares.
The data at some instance of time in column is: 2.00,3.00,32.08,29.02,123.9

The Sum Function Gives me : 190 instead of 190.00 as its default behavior considers this number as integer and eats the Trailing zeroes.. But If the Output is Something of sort of 125.00256 then it displayes it with correct decimals But if the output is 2563.36200, then also it displays it like 2563.362.

After prolonged discussion on DevExpress XtraReport on Various forums.. I found the solution..

Steps:
Change the Summary_Function type of the XRLabel to CUSTOM:

Now Write Code on the following event:
And write the following Code:


private void xrLabel10_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {           
            int decimalCount = 0; 
            double sumOfValues = 0;
            string formatString = String.Empty;
            foreach (var value in e.CalculatedValues)
            {
                if (Convert.ToString(value) != String.Empty)   //check for null values
                {
                    sumOfValues = sumOfValues + Convert.ToDouble(value);   //calculate the sum of values
                      if (Convert.ToString(value).Split('.').Length == 2)  //check for only one decimal Place in data
                          if (Convert.ToString(value).Split('.')[1].Length > decimalCount) //calculating number of decimal places
                                decimalCount = Convert.ToString(value).Split('.')[1].Length;                   
                }
            }
            formatString = formatString.PadRight(decimalCount, '0');     
            e.Result =sumOfValues.ToString("#,##0." + formatString);  //thousand separator + decimal Count
            e.Handled = true;
        }


Hence i over-ride the default behavior of the Method to Solve this Issue.. And beleive me.. It Solved many Similar Concerns for my Client...


No comments:

Post a Comment