Link to home
Start Free TrialLog in
Avatar of David Svedarsky
David SvedarskyFlag 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!
Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

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

Avatar of David Svedarsky

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?
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
ASKER CERTIFIED SOLUTION
Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!