2012-10-14 26 views
2

考慮我下面XtraReport如何在DevExpress的計算條件的總和xtrareport

enter image description here

它會生成以下的輸出預覽時。

enter image description here

通知佔總成績的報告頁腳即125 我的問題是和:我將如何計算的唯一領域,其中的結果是p的總和。 因此,在這種情況下TotalMarks的總和應該是75.

回答

4

我通過添加calcultedfield和在表達式編輯器分配

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

找到其解決方案。

1

據我所知,我會通過摘要標籤的BeforePrint事件手動執行此操作。用代碼計算所需的總和,並將結果分配給標籤的Text屬性。如果您可以在報表中明確地訪問數據源(例如,如果使用IList數據源),則此方法運行良好。

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; 
} 

與其他手動解決方案,你可以通過處理細節帶的BeforePrint事件打印報表中總結了所需的值,然後分配相若方式計算的總的總和標籤在其BeforePrint事件上面。這種方法對於更復雜的數據源更加高效和靈活。

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; 
} 

不幸的是,我不知道任何可以純粹由設計師應用的解決方案。希望能幫助到你。

+2

兩天前我問了這個問題,並找到了解決方法,在表達式編輯器中添加一個calcultedfield並賦予這個「Iif([Result] ='P',[TotalMarks],0)」公式,和我爲125結果所做的一樣。任何方式非常感謝 – kashif

+0

是的,這是一個更優雅的解決方案。我有一個更復雜的場景,我不得不使用代碼。無論如何,我希望有一天會有所幫助。 –