2013-06-12 73 views
3

我使用绑定到数据源的DevExpress xtragrid ...一切都很好。我将添加1个未绑定的列(余额),以保存计算结果。当“借方”和/或“贷方”列在网格中的任何位置发生更改时,“余额”列必须重新计算。鉴于可能有大量的记录,我希望循环声明不会是我唯一的选择。相反,我希望能使用 表达式编辑器的解决方案。DevExpress - xtragrid列运行总计

例如:

dr  cr  balance 
100  0  100 
0  50  50 
0  45  5 

回答

4
  1. 在你所谓的数据源金额创建一个新列。在本专栏中,您将存储的借方和贷方为正值和负值
  2. 将新列添加到您的xtragrid。将其命名为colRunningBalance
  3. 将列的UnboundType设置为十进制。这告诉网格 您将自己处理数据
  4. 将以下事件添加到网格的表单中。该代码是在VB.NET,但它应该是很容易转换成任何语言

    Private Sub gridView_CustomUnboundColumnData(sender As System.Object, e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles 
    gvCash.CustomUnboundColumnData 
          Dim view = DirectCast(sender, GridView) 
    
         If e.Column.FieldName = "colRunningBalance" And e.IsGetData Then 
          Dim total = 0D 
          For i As Integer = -1 To e.ListSourceRowIndex - 1 
           total += CDec(view.GetListSourceRowCellValue(i + 1, "Amount")) 
          Next 
          e.Value = total 
         End If 
        End Sub