我有发票需求汇总单元gridview
Number Description Price Qty Total Discount Grand Total
5050 Screen 50 1 50 5 45
6060 Case 100 2 200 50 150
当我更改price
,qty
或discount
的值时,我需要它来动态计算总计和总计。
答案 0 :(得分:0)
我做了一个小例子(https://github.com/anderson-rancan/stackoverflow_31950624),所以:
使用属性,因此您的数据绑定可以正确使用您的代码
Private _description As String
Public Property Description() As String
Get
Return Me._description
End Get
Set(ByVal value As String)
If Not (value = Me._description) Then
Me._description = value
Me.NotifyPropertyChanged()
End If
End Set
End Property
使用INotifyPropertyChanged更新您的表单和其他结构
Public MustInherit Class BusinessObject
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
在您的商务对象中添加您的商务规则,以便他们自行更新
Private Sub Recalculate()
Me.GrandTotal = Me._shopItems.Sum(Function(item) item.GrandTotal)
End Sub
将数据绑定添加到表单对象
Me._binding = New BindingSource()
Me._binding.DataSource = New Shop()
Me._binding.DataMember = "ShopItems"
Me.ShopItemsDataGridView.DataSource = Me._binding
Me.ShopItemsDataGridView.AutoGenerateColumns = True
使用这些方法,您可以自动重新计算总数并更新表单。
如果您需要更多信息,请使用Google for WinForms DataBinding或类似的东西。