Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

VB
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
Updated 20-Feb-13 20:43pm
v2

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 :)
 
Share this answer
 
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..
[no name] 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
[no name] 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
C#
DataGridView1.Rows(j1).Cells("TOTAL").Value = total

After that put
total=0
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900