Click here to Skip to main content
12,509,269 members (61,871 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
Hi all,

I just need a help regarding datagridview control in vb.net


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
--------------------------
1
3
4
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..
Posted 8-Apr-11 19:03pm
Comments
rajantandel 12-Apr-11 1:47am
   
thanks to all Specailly Andy_L_J.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Take a look at Tip #7 from this page.[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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");
or
dataTable.Columns.Add("grand_total", typeof(System.Double), "sum(amount)");
  Permalink  
Comments
RaviRanjankr 18-Nov-11 15:23pm
   
5+
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

This[^] could help.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

And if the data is coming from a datatable you can use Compute[^] method
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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)
  Next
End With
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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

Next
txtTotlePur.Text = totalsum.ToString()
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

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"))
Next
End With
lblTotal.Text = tot.ToString

Sunil Bhagwat
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160927.1 | Last Updated 10 Jun 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100