Click here to Skip to main content
15,749,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I just need a help regarding datagridview control in

I have datagridview in my form ..i had bind it and i want to perform sum operation of different cell..

for eg:-
i have a "AMOUNT" column in my datagrideview and i want to add those "AMOUNT" cell value.. and display it in a textbox..

my datagridview looks like below..
amount | rate | total
so if i want the sum of "AMOUNT" ie:- "8" in a textbox.. then wat code should i write.. can u pleas help me out any one..pls..
rajantandel 12-Apr-11 1:47am    
thanks to all Specailly Andy_L_J.

Take a look at Tip #7 from this page.[^]
Share this answer
Share this answer
If you need to display your sum of all rows from GridView then follow any of the solutions above.
And if you want to calculate the total using amount*rate, then add data-column to your data-source data-table as;
dataTable.Columns.Add("total", typeof(System.Double), "amount*rate");

dataTable.Columns.Add("grand_total", typeof(System.Double), "sum(amount)");
Share this answer
RaviRanjanKr 18-Nov-11 15:23pm    
This[^] could help.
Share this answer
And if the data is coming from a datatable you can use Compute[^] method
Share this answer
This will get you started...

Dim tot As Double = 0
With dgv
  For Each d As DataGridViewRow In dgv
    tot += CDbl(d.Cells("Amount").Value)
End With
Share this answer
Dim totalsum As Integer
For i As Integer = 0 To dst.Tables(0).Rows.Count - 3
totalsum += dst.Tables(0).Rows(i).Item("Purches_Item")

txtTotlePur.Text = totalsum.ToString()
Share this answer
Dear All

At row level one can use below codes for getting totals of colum using button click or
Private Sub Table1DataGridView_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellEndEdit
event to get total

Dim tot As Double = 0
With Table1DataGridView
For Each d As DataRowView In Table1BindingSource
tot += CDbl(d.Row("Amount"))
End With
lblTotal.Text = tot.ToString

Sunil Bhagwat
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900