Avatar of dave_sky
dave_sky
Flag for United States of America asked on

Display sum of databound datagridview column in textbox

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!
Visual Basic.NET

Avatar of undefined
Last Comment
dave_sky

8/22/2022 - Mon
Jesus Rodriguez

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

dave_sky

ASKER
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?
Jesus Rodriguez

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Jesus Rodriguez

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dave_sky

ASKER
Thank you!