i just want to display datagrid value to label but i am getting problem after passing ten line.

        Label1.Text = Me.DataGridView1.CurrentRow.Cells(0).Value.ToString.Trim Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick

        Label2.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
        Label3.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString
        Label4.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString
        Label5.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString
        Label6.Text = DataGridView1.CurrentRow.Cells(5).Value.ToString
        Label7.Text = DataGridView1.CurrentRow.Cells(6).Value.ToString
        Label8.Text = DataGridView1.CurrentRow.Cells(7).Value.ToString
        Label9.Text = DataGridView1.CurrentRow.Cells(8).Value.ToString
        Label10.Text = DataGridView1.CurrentRow.Cells(9).Value.ToString
        Label11.Text = DataGridView1.CurrentRow.Cells(10).Value.ToString
        Label12.Text = DataGridView1.CurrentRow.Cells(11).Value.ToString
        Label13.Text = DataGridView1.CurrentRow.Cells(12).Value.ToString
        Label14.Text = DataGridView1.CurrentRow.Cells(13).Value.ToString
        Label15.Text = DataGridView1.CurrentRow.Cells(14).Value.ToString
        Label16.Text = DataGridView1.CurrentRow.Cells(15).Value.ToString
        Label17.Text = DataGridView1.CurrentRow.Cells(16).Value.ToString
        Label18.Text = DataGridView1.CurrentRow.Cells(17).Value.ToString

    End Sub

after the label10.text i get error like this when clicking the datagrid

this was the error when i click datagridview
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Means Cell 9 does not exist
here's a code i wrote it'll simplify your work (less codes :D)

        For i = 1 To DataGridView1.Columns.Count - 1
            Dim label As Label = CType(Me.Controls("label" & i + 1), Label)
            If Not DataGridView1.CurrentRow.Cells(i).ToString = "" Or Nothing Then
                label.Text = DataGridView1.CurrentRow.Cells(i).Value
            End If
        Next

Edited 3 Years Ago by oussama_1

This article has been dead for over six months. Start a new discussion instead.