Solved

Display sum of databound datagridview column in textbox

Posted on 2013-12-26
5
2,736 Views
Last Modified: 2013-12-27
Software: Visual studio VB.net & sql server 2005.

I need sample code to display the sum of a databound datagridview column in a textbox.

Thanks!
0
Comment
Question by:dave_sky
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39740671
Dim ColSum as double=0
For Each Rw as DataGridViewRow in DataGridview1.Rows
 ColSum+=Cdbl(iif(Rw(0) is dbnull.value,0,Rw(0)))  'Gets the values for each cell with the column index 0 and sum each one
Next
TextBox1.Text=ColSum.ToString

Open in new window

0
 

Author Comment

by:dave_sky
ID: 39740925
I tried this:

Dim RunningTotals As Double = 4
        For Each Rw As DataGridViewRow In TblBreakEvenDataGridView.Rows
            RunningTotals += CDbl(IIf(Rw(0) Is DBNull.Value, 0, Rw(0)))  'Gets the values for each cell with the column index 0 and sum each one
        Next
        TextboxTotals.Text = RunningTotals.ToString

Open in new window

And got this error: Error      3      Class 'System.Windows.Forms.DataGridViewRow' cannot be indexed because it has no default property.

Any ideas??

Should the Column RunningTotals be unbound?
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39741234
Can you post the grid view bounding structure for the colums that you want to sum. If you want to sum 2 colums and show the result in a 3 one then get the sum result in the query and later bound it if not add a  3 Colin and in the event of day abound do the sum and add it to the 3rd colum. Send me the grid and the bound query that populate it and I will check it
0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 39741238
I made a small mistake on the code, Check it here

Dim RunningTotals As Double = 4
        For Each Rw As DataGridViewRow In TblBreakEvenDataGridView.Rows
            RunningTotals += CDbl(IIf(Rw.cells(0).value Is DBNull.Value, 0, Rw.cells(0).value))  
          'Gets the values for each cell with the column index 0 and sum each one
        Next
        TextboxTotals.Text = RunningTotals.ToString 

Open in new window

0
 

Author Closing Comment

by:dave_sky
ID: 39742428
Thank you!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question