如何计算列中所有值的总和并将其显示在label.content中。
创建专栏:
Dim c7 As New DataGridTextColumn()
c7.Header = "Iznos"
c7.Width = 100
c7.Binding = New Binding("Iznos")
gridRacun.Columns.Add(c7)
创建项目的类:
Class Item
Private _Cijena As String
Private _Rabat As String
Private _PC As String
Private _Iznos As String
Property Kolicina() As Integer
Get
Return _Kolicina
End Get
Set(ByVal value As Integer)
Me._Kolicina = value
End Set
End Property
Property Cijena() As Decimal
Get
Return _Cijena
End Get
Set(ByVal value As Decimal)
Me._Cijena = value
End Set
End Property
Property Iznos() As Decimal
Get
Return _Iznos
End Get
Set(ByVal value As Decimal)
Me._Iznos = Me._Kolicina * Me.Cijena
End Set
End Property
End Class
向datagrid添加项目:
Dim item = New Item With {.Cijena = Globals.cijenaTempG, .Rabat = Globals.rabatG, .PC = 1.0, .Iznos = 1.0}
gridRacun.Items.Add(item)
我想计算名为“Iznos”的列的SUM并将其显示在labelSum.content中
答案 0 :(得分:0)
我认为您可以迭代datagridview的行并计算总和
dim sum = 0
for each row as datagridviewrow in gricRacun.rows
dim cellValue as double
if not double.tryparse(row.cells(c7.index).value,cellValue) then cellValue = 0
sum += cellValue
next row
您可以在文本更改事件和其他事件中触发它并分配您想要的位置
答案 1 :(得分:0)
该列绑定到项目的属性。对属性求和并将其公开给视图,而不是关注网格本身。
' Calculate the sum (using Linq).
Dim sum = gridRacun.Items.OfType(Of Item)().Sum(Function(item) item.Iznos)
' Update the label.
labelSum.Content = sum
现在剩下的就是找出何时触发计算。尝试收听集合何时更改,或者是否更新了任何项目的属性。