13,085,525 members (57,923 online)
Rate this:
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:

## 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 :)
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)

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

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:

## Solution 2

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

After that put
`total=0`
v2
Navas Khanj 21-Feb-13 4:51am

Top Experts
Last 24hrsThis month
 OriginalGriff 260 Jochen Arndt 165 Kornfeld Eliyahu Peter 150 BillWoodruff 90 CPallini 80
 OriginalGriff 3,047 Graeme_Grant 1,369 ProgramFOX 1,367 ppolymorphe 1,217 Kornfeld Eliyahu Peter 1,179