Question

Consider my following XtraReport

enter image description here

It generates following output when previewed.

enter image description here

Notice the sum of Total marks in the report footer i.e 125. My Question is: How will I calculate the sum of the only fields where result is p. Thus in this case sum of TotalMarks should be 75.

Was it helpful?

Solution

I found its solution by adding a calcultedfield and assigning

Iif([Result]='P',[TotalMarks],0 )

in expression editor.

OTHER TIPS

As far as I know, I would do it manually via the summary label's BeforePrint event. Calculate the required sum with code, and assign the result to the Text property of the label. This works well if you can explicitly access your datasource in the report (for example if you use IList datasource).

private void sumLabel_BeforePrint(object sender, BeforeEventArgs e) {
  XRLabel lbl = sender as XRLabel;
  if (lbl == null) return;
  int sum = myList.Where(item => item.Result == "P").Sum(item => item.TotalMarks);
  lbl.Text = sum;
}

With another manual solution you could summarize the required values during printing the report by handling the Detail band's BeforePrint event, and then assign the calculated sum to the sum label in its BeforePrint event similarily to above. This approach is more efficient and flexible for more complicated datasources.

int currentSum = 0;
private void Detail_BeforePrint(object sender, BeforeEventArgs e) {
  var currentRow = GetCurrentRow() as MyData;
  if (currentRow == null) return;
  if (currentRow.Result == "P")
    currentSum += currentRow.TotalMarks;
}

private void sumLabel_BeforePrint(object sender, BeforeEventArgs e) {
  XRLabel lbl = sender as XRLabel;
  if (lbl == null) return;      
  lbl.Text = currentSum;
  // invalidate the currentSum for the case of any further report pringing process
  currentSum = 0;
}

Unfortunately I don't know about any solution which could be applied purely by designer. Hope it helps.

Though kashif Solution is not show the Sum value,but main logic is there. If you want Sum use Sum() function :-

Sum(Iif([Result]='P',[TotalMarks],0 ))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top