user2476666
user2476666

Reputation: 31

DevExpress - xtragrid column running totals

I am using DevExpress xtragrid that is bound to a data source... all is fine there. I am adding 1 unbound column (balance) that will hold the result of a calculation. The 'balance' column MUST recalculate when the debit and / or credit column changes anywhere in the grid. Given there may be a large amount of records I am hoping a loop statement will not be my only option. Rather I was hoping for a solution using the Expression editor.

example:

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

Upvotes: 3

Views: 3833

Answers (1)

Rad
Rad

Reputation: 8381

  1. Create a new column in your data source called Amount. In this column you will store both the debits and credits as positive and negative values
  2. Add a new column to your xtragrid. Name it colRunningBalance
  3. Set the UnboundType of the column to decimal. This tells the grid you will be processing the data yourself
  4. Add the following event to your form for the grid. The code is in VB.NET but it should be pretty easy to convert to any language

    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
    

Upvotes: 4

Related Questions