Calculate subtotal and grandtotal on datagridview based on category column

Last Reply 8 days ago By dharmendr

Posted 9 days ago

Hellow hi everyone in here!

Can somehow help edit my vb.net on how to calculate subtotal and grandtotal as I asked in my question.

Im new to VB.NET programming but have more interest trying things such as programming simple apps using microosft visual studio. In my datagridview shown below which is not datasource bound i want to write some vb.net codes that will calculate subtotal(s) and finally grandtotal(s) and show the totals to user as you can see in colored cells. For almost 3 days ago i have tried (see my codes below) but the codes i wrote does not produce correct result.i mean the summation commingout is not correct. Can anyone help me my mistakes to make that code work?? please help i'm ready to learn from you friends.

I want this  event to fire immediately under Datagridview1.CellEndEdit when i complete a cell edit under the column named 'This Variation Quantinty' in datagridview because the other columns to the left of this columns are calculated columns they depend on the input given in the 'This Variation Quantinty'. The codes i tried which produces wrong results:

Public Class Contract_Variation_Details    
    Public index, kz As Integer
    Private Sub GetRunningTotal()
        For Each ROWSWITHDATA As DataGridViewRow In DgvVariationDetails.Rows
            Dim SUM As Decimal
            'check if datagridview rows cells (0) in datagridview contains text 'Sub-Total'
            If ROWSWITHDATA.Cells(0).Value = "Sub-Total" Then
                'store the index of that cell to a variable named 'index'
                index = ROWSWITHDATA.Cells(12).RowIndex
            ElseIf ROWSWITHDATA.Cells(0).Value <> "Sub-Total" Then
                'store the index of that cell to a variable named 'index'
                kz = ROWSWITHDATA.Cells(12).RowIndex
            End If
            For cell As Integer = 0 To DgvVariationDetails.Rows.Count - 2 Step +1
                'this is the formula to get sum of column value where its cell(0) doesnot contain the word 'Sub-Total'
                SUM += CDec(DgvVariationDetails.Rows(kz).Cells(12).Value)
            Next
            DgvVariationDetails.Rows(index).Cells(12).Value = SUM
        Next
    End Sub
    Private Sub DgvVariationDetails_CellEndEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DgvVariationDetails.CellEndEdit
        Try
            GetRunningTotal()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
End Class

 

My datagridView looks like as shown below:

 SN            Category           Quantinty   [ThisVariationQantinty]   TotalQty   Total amount

1                 Roadworks         4.5                           3.0                         7.5               15,000.00

2                 Roadworks          1.8                           4.0                         5.8              12,000.00

Sub-Total  ...................................................................................... 13.3              27,000.00

3               DrainageWorks    1.9                            1.2                        3.11                5,000.00

4               DrainageWorks     1.0                            1.5                        2.50                7,000.00

Sub-Total  .......................................................................................  5.61               12,000.00

5              Culverts                   38.2                           1.2                       39.4               1,250,000.00

6              Culverts                    9.5                             0.5                       10.00             5,000,000.00

Sub-Total ........................................................................................   49.40             6,250,000.00

Grand-Total ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪     6,289,000.00

 

You are viewing reply posted by: pandeyism 9 days ago.
Posted 9 days ago

Hi CLEMENCEM,

Please share the code how you are displaying subtotal and grandtotal on datagridview.