tags:

views:

506

answers:

2

Hello,

I have a L2S SalesOrder entity with a child set of LineItems. Using winforms, on my Sales Order form I have a datagridview bound to the LineItems and it works like a dream. If the user changes a LineItem quantity (for instance), my LineItem partial class handles the OnQuantityChanged and recalculates the LineItem SubTotal field and the grid is automagically updated. I'm ecstatic with how clean and neat it is.

Now I would like to have a calculated field on my SalesOrder object called "Total" that is the total of all the LineItems' SubTotal fields (plus a few other things like tax and shipping, but don't worry about that for now). What is the best/cleanest way for the SalesOrder's Total field to be automagically updated whenever a LineItem is changed? If someone could just point me in the right direction on this I would be most appreciative.

Don't know if it makes a difference but I'm using Rick Strahl's Linq To SQL business framework (and it is fantastic, BTW!).

Thanks!

A: 

You might want to check out Bindable LINQ:

Bindable LINQ is a set of extensions to LINQ that add data binding and change propagation capabilities to standard LINQ queries.

Codebrain
A: 

This was actually easier than I thought. I added the ReCalculate() to my SalesOrder and then I can just call it from my LineItem like this...

Partial Class SalesOrderLineItem
    Private Sub OnQuantityChanged()
        RefreshMySubTotal()
    End Sub
    Private Sub OnUnitPriceChanged()
        RefreshMySubTotal()
    End Sub
    Private Sub RefreshMySubTotal()
        Dim dNewSubtotal As Decimal = 0
        dNewSubtotal = Me.Quantity * Me.UnitPrice * (1 - (Me.Discount * 0.01))
        If Me.SubTotal <> dNewSubtotal Then
            Me.SubTotal = dNewSubtotal
            If Me.SalesOrder IsNot Nothing Then Me.SalesOrder.ReCalculate()
        End If
    End Sub
Monty