将文本框绑定到数据表列的总和 [英] Bind a TextBox to the Sum of a DataTable Column
问题描述
我想在我的网格下面有一个页脚,以显示有关数据的一些统计信息.
I would like to have a footer under my grid to show some stats about the datas.
例如某些列的总和,另一些列的平均值.
Such as Sum of some columns and average of some other.
我发现有两件事很有趣,可以帮助我做到这一点,将数据框绑定到bindingSource,但是只有选择的行显示在textBox中...
I've found two things intersting to help me do this, binding the databox to a bindingSource, BUT it's only the selected line that is shown into the textBox...
myTextBox.DataBindings.add("Text", myGrid.DataSource,"Weight")
并获取网格中某一列的总和,但是如果我更改网格,它不会更新:S
And getting the Sum of a column in the grid, BUT it doesn't update if I change the grid :S
myTextBox.Text = myGrid.DataSource.DataSource.DataSet.Tables(0).Compute("Sum(Weight)","")
问题
有两种方法都可以吗?
Question
Is there a way to have both?
像BindingSource一样自动更新,而像Compute一样自动求和?
The auto updates as with BindingSource and the Sum as with Compute?
推荐答案
由于您将DataTable用作绑定源,因此只需连接RowChanged事件并在那里计算更改:
Since you are using a DataTable as the binding source, you can just wire the RowChanged event and compute changes there:
Private Sub myTable_RowChanged(ByVal sender As Object, _
ByVal e As DataRowChangeEventArgs) _
Handles myTable.RowChanged
myTextBox.Text = myTable.Compute("SUM(Weight)", String.Empty)
End Sub
这篇关于将文本框绑定到数据表列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!