hi, I have dgv1 that contains amount column, and dgv2 that also have amount aolumn.
I have following code to sum total vlaues of dgv1.
but i want display both column vlaues from both dgv in to single textbox.
how can i do this?

Dim total As Integer
        For Each row As DataGridViewRow In ReportDataGridView.Rows()
            total += row.Cells(6).Value
        Next
        txtTotalAmount.Text = total

Dani AI

Generated

A concise addendum to the working solution from and with a few robustness tips and two alternative approaches (one that reads the grid UI, one that uses the bound DataTable).

Use Decimal for money (not Integer), skip the DataGridView "new" row, handle Nothing/DBNull and values that are strings with currency symbols, and prefer column names ("Amount") over hard-coded column indexes so column reordering does not break the code. When the grid is bound, prefer computing on the underlying DataTable (faster and more reliable); if users may have unfinished edits, commit them first (for example call BindingSource.EndEdit()).

A LINQ-based, UI-read approach that tolerates blank cells and currency-formatted strings:

Imports System.Globalization

Dim parseStyles = NumberStyles.Currency Or NumberStyles.Number
Dim ci = CultureInfo.CurrentCulture

Dim SumGrid = Function(g As DataGridView) As Decimal
    Return g.Rows.Cast(Of DataGridViewRow)().
        Where(Function(r) Not r.IsNewRow).
        Select(Function(r)
                   Dim raw = r.Cells("Amount").Value
                   Dim val As Decimal = 0D
                   If raw IsNot Nothing Then Decimal.TryParse(raw.ToString(), parseStyles, ci, val)
                   Return val
               End Function).Sum()
End Function

Dim total = SumGrid(PatientDetailsDataGridView) + SumGrid(FollowUpRegisterDataGridView)
txtTotalAmount.Text = total.ToString("N2")

If the grids are bound to DataTables, using Compute is simpler and faster:

Dim dt1 = TryCast(PatientDetailsDataGridView.DataSource, DataTable)
Dim dt2 = TryCast(FollowUpRegisterDataGridView.DataSource, DataTable)

Dim s1 As Decimal = 0D
If dt1 IsNot Nothing Then
    Dim o = dt1.Compute("Sum([Amount])", Nothing)
    If o IsNot DBNull.Value Then s1 = Convert.ToDecimal(o)
End If

Dim s2 As Decimal = 0D
If dt2 IsNot Nothing Then
    Dim o = dt2.Compute("Sum([Amount])", Nothing)
    If o IsNot DBNull.Value Then s2 = Convert.ToDecimal(o)
End If

txtTotalAmount.Text = (s1 + s2).ToString("C2")

Quick troubleshooting notes: ensure the column name matches exactly, set Option Strict On to catch implicit conversions, call BindingSource.EndEdit() when necessary, and skip rows where IsNewRow is True so the blank add-row does not affect the sum.

Recommended Answers

All 7 Replies

Hi

Use the same code to grab the total for the amount column in your second data grid view and then add both totals together and assign to your text box.

HTH

how can i add both totals together and assign it to single textbox.

here is what i get the total of 2 dgv in two different textboxs , but I want the total in single textbox.

here is the code.

Dim total As Integer
            For Each row As DataGridViewRow In PatientDetailsDataGridView.Rows()
                total += row.Cells(8).Value
            Next
            txtTotalAmount.Text = total

Dim total1 As Integer
            For Each row As DataGridViewRow In FollowUpRegisterDataGridView.Rows()
                total1 += row.Cells(8).Value
            Next
            TextBox1.Text = total1

Hi

Add total and total1 together and assign to your text box:

txtTotalAmount.Text = (total + total1).ToString

HTH

Hi so my code is:-

Dim total As Integer
        For Each row As DataGridViewRow In PatientDetailsDataGridView.Rows()
            total += row.Cells(8).Value
        Next

        Dim total1 As Integer
        For Each row As DataGridViewRow In FollowUpRegisterDataGridView.Rows()
            total1 += row.Cells(8).Value
        Next
        txtTotalAmount.Text = (total + total1).ToString

Is it giving you the desired output or is there a question here?

yes i got what i want.
thnks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.