Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 352
user2476666 Avatar asked Oct 28 '25 21:10

user2476666


1 Answers

  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
    
like image 62
Rad Avatar answered Oct 31 '25 11:10

Rad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!