问题
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