Bind a TextBox to the Sum of a DataTable Column

谁说胖子不能爱 提交于 2021-02-19 08:15:08

问题


Context

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.

Half solutions I've found

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")

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?

The auto updates as with BindingSource and the Sum as with Compute?


回答1:


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


来源:https://stackoverflow.com/questions/12097168/bind-a-textbox-to-the-sum-of-a-datatable-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!