0

Hi,
i want to total the values in column9 in Patientdatagridview to a textbox1.
here is my code but it displayes each value, not the total.
e.g. if values are 200, 200, 200 then it should display 600
but here it says 0200200200

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tot As String = 0
        For i As Integer = 0 To PatientDetailsDataGridView.RowCount - 1
                       tot += PatientDetailsDataGridView.Rows(i).Cells(8).Value
                    Next
        If tot = 0 Then
            MessageBox.Show("No Records Found")
        End If
        TextBox1.Text = tot.ToString()

    End Sub
2
Contributors
2
Replies
9
Views
1 Year
Discussion Span
Last Post by Satyam_1
0

"+" Charactor always concatinates two string type values i.e "200" +"200" always give you the result 200200 instead of 400, because you treat them as string type. If you declare them as numeric type then it will give you 400.

Your mistake is at line no 2. Declare tot as double type instead of string type.

Dim tot As Double = 0#

and the loop should be

For i As Integer = 0 To PatientDetailsDataGridView.RowCount - 1
    tot += Val(PatientDetailsDataGridView.Rows(i).Cells(8).Value)
Next

Hope it should help you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.