Hi guys,
im having trouble summing up the column in my (unbound) datagridview, i can add to the DGV fine, but when trying to sum up a column it says "Input string was not in a correct format."

heres my code

  Private Sub btn_Add_Click(sender As System.Object, e As System.EventArgs) Handles btn_Add.Click
        DataGridView1.Rows.Add(cbox_Product.Text, tbox_Sale_Price.Text, tbox_Qty.Text, tbox_Discount.Text, tbox_SubTotal.Text)

        If DataGridView1.Rows.Count > 0 Then
            tbox_Gross.Text = total().ToString("c")
        End If


    End Sub


Private Function Total() As Double
        Dim tot As Double = 0
        Dim i As Integer = 0
        For i = 0 To DataGridView1.Rows.Count - 1
            tot += Convert.ToDouble(DataGridView1.Rows(i).Cells(4).Value)
        Next i
        Return tot
    End Function

any ideas guys?
cheers :)

hi guys nevermind, i was being thick, my tbox_Subtotal (before going into the datagridview) was wrong i had it from a variable to tbox like
tbox = "£ " & variable
instead of
ToString("C")
d'oh

Probably an empty cell.

try: if Not String.IsNullOrEmpty(DataGridView1.Rows(i).Cells(4).Value.ToString()) Then tot += Convert.ToDouble(DataGridView1.Rows(i).Cells(4).Value)

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.