Running balance in a column

5 posts, 0 answers
  1. Mark
    Mark avatar
    5 posts
    Member since:
    Feb 2016

    Posted 09 Feb Link to this post

    Hi:

    I've pretty much gathered that this isn't supported natively, but surely there's a way to make it work with expressions or event handling. Or something.

    Each row in my grid has a Credit column and a Debit column. It needs to have a Balance column that takes the net of the Credit and Debit columns in a row and then adds that value to the balance of all the rows before it. I suppose the quintessential example of this functionality would be a checkbook register. (

    Credit - Debit - Balance

    0 - 100 - -100

    50 - 0 - -50

    200 - 0 - 150

     

    Thanks for the help.

  2. Stefan X1
    Admin
    Stefan X1 avatar
    514 posts

    Posted 11 Feb Link to this post

    Hello Mark,

    In general, such operations should be implemented on data level. For your scenario in particular, you can define a calculated property which will serve as the Balance column. Its value will be updated based on the values of the Credit and Debit properties of a given object.

    As for updating the previous rows, the correct approach depends on the specific requirements. For example, if the previous rows need to be updated when the user commits an edit, the RowEditEnded/CellEditEnded events that RadGridView exposes can be used. Can you please share some more details on this?

    Best Regards,
    Stefan X1
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Mark
    Mark avatar
    5 posts
    Member since:
    Feb 2016

    Posted 11 Feb Link to this post

    Hi:

    I can see that argument that summing the credit and debit value of a particular row could be done at the data level or as a read-only property on the model or whatever. I need to do more than that. I need to add that value to the sum of that calculation from all the previous rows, as well.

    I'm attaching a screenshot from Excel. What I need to do in my grid is implement the green balance column. Additionally, if a user enters a new record dated, say, 1/20/206 then I need that to add its net value to the balance as of 1/15/2016 and cascade that change in balance through to the 1/31/2016 row and to whatever other rows may also exist.

    Is it your recommendation that that be done at the data level? That I should do a CommitEdit for the new row and then recalculate the balance column at the data level? The balance isn't stored anywhere. It's 100% calculated.

    What I sort of expected the approach to be is something along the lines of how I'd have handled OnItemDataBound on an ASPX page a while back: basically declare a variable and do the math as each row was databound.

    I'm hoping this explanation and the screenshot make more sense than my original post seems to have. The balance column isn't simply the balance of that row. That would be trivial. The balance the sum of all the balances of all the rows up to that point. It's not that that is especially harder, it's that I'm hoping to be able to tell the grid how to do the calculating rather than do all of the calculations in my object and just bind the grid to that field.

    Thanks,

    Mark

  5. Stefan X1
    Admin
    Stefan X1 avatar
    514 posts

    Posted 12 Feb Link to this post

    Hi Mark,

    Thanks for the clarification.

    Actually, the calculation and update of the Balance column on all rows should be done on data level. As the rows of RadGridView are not aware of each other, a slight modification in your data structure should be implemented. Basically, the idea is that each object should hold a reference to the previous and next element, as in a doubly linked list structure. When the balance property of a given object is being calculated based on its credit and debit ones, you should be able to iterate through the previous and next nodes of the linked list and recalculate their balance property as well.

    Please note also, that your business object should implement INotifyPropertyChanged interface in order the UI to be updated when the value of a given property is recalculated.

    I hope this suggestion help you achieve the desired behavior of the control.

    All the best,
    Stefan X1
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Mark
    Mark avatar
    5 posts
    Member since:
    Feb 2016

    Posted 12 Feb in reply to Stefan X1 Link to this post

    Hi Stefan:

    I'm not entirely sure that I'm keen on doing this in my objects. Those objects, and the viewmodels, already implement INotifyPropertyChanged so that wouldn't be an issue, though.

    One of your... competitors has a code sample of how to achieve what I need with their grid. I'll see if I can adapt their approach to work with your gridview. If not, implementing something like a doubly linked list wouldn't be the end of the world. Maybe I was just too hopeful that the M and VM part of my MVVM were already done.

    Thanks for the suggestion. I'm sure I'll get it going one way or the other.

    -- Mark

Back to Top
UI for WPF is Visual Studio 2017 Ready