Click here to Skip to main content
13,085,525 members (57,923 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi.. All How To Calculate Total.. I Expect This OUTPUT


Regno Name Class Jan Feb Mar Apr TOTAL
A100 AAA I 500 150 120 54 824
A200 BBB I 450 50 0 0 500


Please Tell me.. how to i do..

Here I am Using This CODE

For j1 As Integer = 0 To DataGridView1.Rows.Count - 1
            For ij As Integer = 3 To DataGridView1.Columns.Count - 1
                With Me.DataGridView1.Rows.GetFirstRow(Me.DataGridView1.Rows.Count - 1)
                    If IsDBNull(DataGridView1(ij, j1).Value) <> True Then
                        total = total + DataGridView1(ij, j1).Value
                    End If
 
                End With
            Next ij
            DataGridView1.Rows(j1).Cells("TOTAL").Value = total
        Next j1

Now This CODE SHOW LIKE THIS OUTPUT

Regno Name Class Jan Feb Mar Apr TOTAL
A100 AAA I 500 150 120 54 824
A200 BBB I 450 50 0 0 1324
Posted 20-Feb-13 20:11pm
Updated 20-Feb-13 20:43pm
uspatel30.7K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If you are binding the grid with data using Sql Query then its better to sum up there on query side only likewise


SELECT RegNo , Name , Class , Jan , Feb , Mar , Apr ,(Jan+Feb+Mar+Apr) as Total FROM TABLE




happy coding :)
  Permalink  
Comments
Navas Khanj 21-Feb-13 2:50am
   
Hi Jan,feb,Mar,etc.. This Hearder Texy I am using PIVOT Table so above code is not work for this condiction..

please tell me.. any other way..
CodeMaster_Noob 21-Feb-13 2:57am
   
Your code seems to be working fine..whats the problem that you are facing??
Navas Khanj 21-Feb-13 3:03am
   
Dim strSQL1 As String = "SELECT * FROM ( SELECT [Regno], [FeeType], [SName], [AmountPaid], [Class] FROM FeeTrans) AS DT " & _
" PIVOT(SUM([AmountPaid]) FOR [FeeType] IN ([Jan],[Feb],[Mar],[Apr])) as PT WHERE Class='" & cboClass.Text & "'"
Dim DaAp1 As New SqlDataAdapter(strSQL1, con)
Dim Dset1 As New DataTable
DaAp1.Fill(Dset1)

Dset1.Columns.Add("TOTAL")
DataGridView1.DataSource = Dset1



Dim total As Integer = 0
For j1 As Integer = 0 To DataGridView1.Rows.Count - 1
For ij As Integer = 3 To DataGridView1.Columns.Count - 1
With Me.DataGridView1.Rows.GetFirstRow(Me.DataGridView1.Rows.Count - 1)
If IsDBNull(DataGridView1(ij, j1).Value) <> True Then
total = total + DataGridView1(ij, j1).Value
End If

End With
Next ij
DataGridView1.Rows(j1).Cells("TOTAL").Value = total
Next j1
HI Here I am using Full Code..
Navas Khanj 21-Feb-13 3:04am
   
Please Tell me.. how can i do
CodeMaster_Noob 21-Feb-13 3:53am
   
Rewrite your code
For j1 As Integer = 0 To DataGridView1.Rows.Count - 1
For ij As Integer = 3 To DataGridView1.Columns.Count - 1
With Me.DataGridView1.Rows.GetFirstRow(Me.DataGridView1.Rows.Count - 1)
If IsDBNull(DataGridView1(ij, j1).Value) <> True Then
total = total + DataGridView1(ij, j1).Value
End If

End With
Next ij
DataGridView1.Rows(j1).Cells("TOTAL").Value = total
Next j1

as

For j1 As Integer = 0 To DataGridView1.Rows.Count - 1
total = 0
For ij As Integer = 3 To DataGridView1.Columns.Count - 1
With Me.DataGridView1.Rows.GetFirstRow(Me.DataGridView1.Rows.Count - 1)
If IsDBNull(DataGridView1(ij, j1).Value) <> True Then
total = total + DataGridView1(ij, j1).Value
End If

End With
Next ij
DataGridView1.Rows(j1).Cells("TOTAL").Value = total
Next j1
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

DataGridView1.Rows(j1).Cells("TOTAL").Value = total

After that put
total=0
  Permalink  
v2
Comments
Navas Khanj 21-Feb-13 4:51am
   
Thanks for Your Response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100