datagridview column sum

5k3l3t0r

Member
Joined
Feb 7, 2008
Messages
6
Programming Experience
Beginner
hi all,
it is possible to get a sum (in textbox) of a datagridview column? can someone tell me how?
tkx in advance...
sorry about my english...
 
1) Add a new column to the datatable, with its expression set to SUM([Column Name To Sum])
2) Add a textbox to the form
3) Bind it to the new column in the BindingSource that binds to the datatable

This works because every row in the table gets the SUM() of all the rows, so you get:

a, 1, 6
b, 2, 6
c, 3, 6

even when you navigate through rows, the textbox always says 6 because every row has that value. Edit a value in the summed column and ever row updates to the new value
 
hi again... i try the solution of cjard, and it work but
when i move to the new row in datagridview(blank row)
the total textox is empty too...
can i solve this?
 
Calculating total of columns in a datagridview control

Hi,

Calculating total of columns in a datagridview control

The function CreditTotal() Calculates the total of Credit column of the DataTable ‘PrivateDataTable’ which in turn binds to DataGridView control.
This function returns the total of credit column.

VB.NET:
Private Function CreditTotal() As Double
	Dim mctot As Double
	Dim i As Integer
	For i = 0 To PrivateDataTable.Rows.Count - 1
		mctot = mctot + PrivateDataTable.Rows(i)(2)
	Next i
	Return mctot
End Function

The PrivateDataTable.Rows(i)(2) indicates the third column of the datagridview.
 
hi again... i try the solution of cjard, and it work but
when i move to the new row in datagridview(blank row)
the total textox is empty too...
can i solve this?

Commit the row?

The row has no total value because it hasnt become part of the table and caused a recalculation yet.. If you need it to show a value when an uncommitted row is focused, that might require a bit of work, maybe you can override the proc that gets data for that column and return the most recent value if the actual value is a null (not calculated yet)
 
Back
Top