2 Replies - 1723 Views - Last Post: 21 April 2013 - 07:08 PM Rate Topic: -----

#1 dave_mark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 15-March 13

datagrid view no more index error

Posted 18 March 2013 - 06:48 PM

i had display data in the datagridview.. my next job is to select the data in the current row.. but when theres no more data there is an error that the index is nothing.. how to handle this syntax error.. this is my code..

Private Sub btnP_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnP_Save.Click

        Dim i As Integer


        i = grid.CurrentRow.Index


        Label2.Text = grid.Item(0, i).Value
        Label3.Text = grid.Item(1, i).Value

        If Not grid.CurrentRow.IsNewRow Then
            grid.Rows.Remove(grid.CurrentRow)
        End If
End if

This post has been edited by modi123_1: 18 March 2013 - 07:36 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: datagrid view no more index error

#2 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 201
  • View blog
  • Posts: 702
  • Joined: 01-February 13

Re: datagrid view no more index error

Posted 18 March 2013 - 07:32 PM

Hello dave_mark,
You can just check to see if the row count is greater than 0 and if it is then execute the code like this
    Private Sub btnP_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnP_Save.Click
        If Grid.RowCount > 0 Then

            Dim i As Integer


            i = grid.CurrentRow.Index


            Label2.Text = CStr(grid.Item(0, i).Value)
            Label3.Text = CStr(grid.Item(1, i).Value)

            If Not grid.CurrentRow.IsNewRow Then
                grid.Rows.Remove(grid.CurrentRow)
            End If
        End If


Also the label2 and label3 i fixed with CStr() because it needs to be converted to a string before assigning it to a labels text.

This post has been edited by IronRazer: 18 March 2013 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 dave_mark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 15-March 13

Re: datagrid view no more index error

Posted 21 April 2013 - 07:08 PM

IronRazor.. thanks.. this is helpful
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1