Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27350

VS 2012 [RESOLVED] Datagridview --> Index out of range

$
0
0
Hey,

got a question regarding an error I get while debugging. I created a small form with a datagridview (1 row, 2 columns) with this code
Code:

DataGridView2.Rows.Add("0.0", "0.0")
after a few calculations I would like to update these values each time the user changes the value of a textbox. This is my code (not working - giving me an Index out of range error pointing to the first DataGridView line)
Code:

        If Val(TextBox2.Text) < 0.01 Or TextBox2.Text = "" Then
            DataGridView2.Rows(0).Cells(0).Value = "0.0"
            DataGridView2.Rows(0).Cells(1).Value = "0.0"
        Else
            mvorschub = TextBox2.Text
            DataGridView2.Rows(0).Cells(0).Value = mvorschub
            DataGridView2.Rows(0).Cells(1).Value = sbreite * DataGridView2.Rows(0).Cells(0).Value * kc * (DataGridView2.Rows(0).Cells(0).Value ^ -mc)
        End If

Well I am messed around a bit with this freakin error and got a solution but I don't know why this one is working without an error message

Code:

        If Val(TextBox2.Text) < 0.01 Or TextBox2.Text = "" Then
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                DataGridView2.Rows(0).Cells(0).Value = "0.0"
                DataGridView2.Rows(0).Cells(1).Value = "0.0"
            Next
        Else
            mvorschub = TextBox2.Text
            DataGridView2.Rows(0).Cells(0).Value = mvorschub
            DataGridView2.Rows(0).Cells(1).Value = sbreite * DataGridView2.Rows(0).Cells(0).Value * kc * (DataGridView2.Rows(0).Cells(0).Value ^ -mc)
        End If

There is only 1 row so it's a bit strange I need a for loop...any idea why this one is working?

Viewing all articles
Browse latest Browse all 27350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>