glstack
asked on
Can you manipulate data in a datagridview in real time
I need to be able to take the average of the values in a column in a datagridview after rows have been deleted, without affecting the data source. The code counts the correct number of rows but it takes the average of the data source not the datagridview.
Private Sub ReCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReCalc.Click
Dim count As Integer = 0
Dim total As Double = 0
WSW067DataGridView.EndEdit()
For Each row As DataGridViewRow In WSW067DataGridView.Rows
total += row.Cells(10).Value
Next
While count < WSW067DataGridView.RowCount
count += 1
End While
lblCount.Text = count - 1
lblGoldUsageAverage.Text = total / count
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As the code you show is taking the values for the total and the count directly from the DataGridView, I don't see how the result can be anything different from the average of what's shown in the DataGridView. What am I missing?
And, incidentally, I don't see the need for the loop in lines 12 to 16. Couldn't you just use
lblCount.Text = WSW067DataGridView.RowCoun t - 1
in line 17?
Roger
And, incidentally, I don't see the need for the loop in lines 12 to 16. Couldn't you just use
lblCount.Text = WSW067DataGridView.RowCoun
in line 17?
Roger
Cross-post ;-)
ASKER
This is what I ended up using. Thanks for the help!
Private Sub ReCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReCalc.Click
Dim count As Integer
Dim total As Decimal = 0
WSW067DataGridView.EndEdit ()
For Each row As DataGridViewRow In WSW067DataGridView.Rows
total += row.Cells(10).Value
Next
count = WSW067DataGridView.RowCoun t - 1
lblGoldUsageAverage.Text = total / count
Private Sub ReCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReCalc.Click
Dim count As Integer
Dim total As Decimal = 0
WSW067DataGridView.EndEdit
For Each row As DataGridViewRow In WSW067DataGridView.Rows
total += row.Cells(10).Value
Next
count = WSW067DataGridView.RowCoun
lblGoldUsageAverage.Text = total / count
While count < WSW067DataGridView.RowCoun
count += 1
End While