Hi everyone, im new to this fourm. Ok i'm working on a dgv page, every time i fill out my text boxes ect. i press add transaction button i got.
Information shows up in the dgv, but then theres 2 columns i want to have the total sum show up in 2 seperate textboxes everytime i hit the add transaction button.
I got 2 seperate check boxes plus the 2 seperate textboxes.The one check box is for income the other is for expense. Same with the columns.
If i fill cells 5 first then also cells 4 it works fine.But then if i try to go back to filling out cell5 i get an error` total += Convert.ToDouble(DataGridView1.Rows(i).Cells(4).Value)

Hope this makes enough sense so somone can help. Thnaks!

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ExRB.Checked Then
            DataGridView1.Rows.Add(DateTimePicker1.Text, ComboBox1.Text, TextBox5.Text, TextBox6.Text, "", TextBox.Text)
            Dim total As Double = 0
            For index As Integer = 0 To DataGridView1.RowCount - 1
                If Not IsDBNull(DataGridView1.Rows(index).Cells(5).Value) Then
                    total += Convert.ToDouble(DataGridView1.Rows(index).Cells(5).Value)
                End If
            Next
            Label11.Text = total.ToString
        ElseIf InRB.Checked Then
            DataGridView1.Rows.Add(DateTimePicker1.Text, ComboBox1.Text, TextBox5.Text, TextBox6.Text, TextBox.Text)
            Dim total As Double = 0
            For i As Integer = 0 To DataGridView1.RowCount - 1
                total += Convert.ToDouble(DataGridView1.Rows(i).Cells(4).Value)
            Next
            Label10.Text = total.ToString
        End If
        ComboBox1.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox.Text = ""
    End Sub

Dani AI

Generated

Short answer: the FormatException is coming from trying to convert empty or non-numeric cell values, and the two different Rows.Add(...) calls in your button handler are likely shifting values into the wrong columns. Fix both issues and you will stop getting the "not in a correct format" error.

Make sure every Rows.Add(...) call supplies the same number of values (or set cells by column index) so income and expense always land in the intended columns. When summing, skip the DataGridView new-row placeholder and any DBNull/empty strings, and use a safe parse (TryParse) into Decimal for money values to avoid exceptions and rounding surprises.

Example pattern to recalc totals (call this after adding a row or on cell-value changes):

Private Sub RecalculateTotals()
    Dim incomeTotal As Decimal = 0D
    Dim expenseTotal As Decimal = 0D

    For Each r As DataGridViewRow In DataGridView1.Rows
        If r.IsNewRow Then Continue For

        Dim s As String = If(r.Cells(4).Value, String.Empty).ToString()
        Dim d As Decimal
        If Decimal.TryParse(s, d) Then incomeTotal += d

        s = If(r.Cells(5).Value, String.Empty).ToString()
        If Decimal.TryParse(s, d) Then expenseTotal += d
    Next

    Label10.Text = incomeTotal.ToString("C")
    Label11.Text = expenseTotal.ToString("C")
End Sub

Debug tips tied to earlier replies: follow and set a breakpoint on the failing line and inspect the offending cell.Value (is it "", DBNull, or a non-numeric string?). was right to flag the DBNull/empty case. Also prefer Decimal for money, and consider using numeric controls or validated input so non-numeric text never reaches the grid. Finally, centralize the summing in one routine (like above) so totals stay correct regardless of which branch runs.

Recommended Answers

All 8 Replies

Here's a thing. Your topic doesn't seem to match what you ask in the text below.

So beside that, in the IDE break on the error line and examine variables to see what the issue is.

Saying you have an error tells us little to help solve your problem.
What is the error? What value do some values have? Your stack trace. Please show us that information.

Line # 15

Exception:Thrown: "Input string was not in a correct format." (System.FormatException)
A System.FormatException was thrown: "Input string was not in a correct format."
Time: 06/04/2017 8:41:15 PM
Thread:<No Name>[7696]

Well, that's a start. :)
Now set some breakpoints in this code and see what happens and let us know. A FormatException is usually not that hard to find out.

I wonder why your line #15 isn't wrap like line #7 with If Not IsDBNull... because in case ...cells(4).value is DBNull because of an empty input, an exception will occur.

commented: Nulls are small slug-like creatures of limited intelligence who drain energy. +12

Ok sorry this is not working yet. I will start with a new topic

No, please don't. Why start another thread to go over the same ground again?

<too late> <sigh>

I have closed your other thread, and posted saying that the discussion/help should continue here.

If you have troubles with setting brakpoints:

and how to debug:
read this

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.