Solved

Display sum of databound datagridview column in textbox

Posted on 2013-12-26
5
2,705 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now