Solved

Display sum of databound datagridview column in textbox

Posted on 2013-12-26
5
2,826 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

617 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