i am trying to create a nextrow button to work with a bound datagridview
the problems i have:
1. the row moves down, but the first row is the one who has the focus (the little black triangle is staying to it's left.
2. i am trying to set a condition, and that is if the index = the row before last (last row is always empty in a datagridview) then select the row before last.
3. please try and explain what am i doing wrong - i am a newbie

i am getting an index overflow

Private Sub btnNextRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNextRow.Click
        Dim iTest As Integer
        Dim LastRow
        LastRow = grdAccountTypes.Rows.Count
        grdAccountTypes.SelectionMode = DataGridViewSelectionMode.FullRowSelect
       

        Try
            If iTest >= LastRow Then
                iTest = grdAccountTypes(0, 0).Selected = True

            Else
                iTest = grdAccountTypes.SelectedRows(0).Index + 1
                grdAccountTypes.Rows(iTest).Selected = True
            End If
          
            '   If iTest = LastRow Then
            ' grdAccountTypes(0, LastRow).Selected = False
            '   End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Recommended Answers

All 4 Replies

Hi eladreznik

I think you may find it easier to change a few settings in design view - that is, select the grid in the designer and make these changes in the properties window:

1) set AllowUserToAddRows to False.

2) set MultiSelect to false.

3) If you want, set SelectionMode to fullRowSelect here instead of in button click code


THen try this in your button click:

Try
	   Dim rowCount As Integer = grdAccountTypes.Rows.Count
            If rowCount > 0 Then
                If grdAccountTypes.SelectedRows.Count > 0 Then
                    Dim row As DataGridViewRow = grdAccountTypes.SelectedRows(0)
                    If row.Index < rowCount - 1 Then
                        row = grdAccountTypes.Rows(row.Index + 1)
                        row.Selected = True
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

there is one minor issue the selection indicator the black arrow stays at the first row.

Yes, sorry eladreznik. You have a couple of choices.


1) If you created your grid with a wizard and datasource, it probably uses a binding source - called something like AccountTypesBindingSource. In this case, we can lose the line row.Selected = True and insert AccountTypesBindingSource.MoveNext like this:

Try
            Dim rowCount As Integer = grdAccountTypes.Rows.Count
            If rowCount > 0 Then
                If grdAccountTypes.SelectedRows.Count > 0 Then
                    Dim row As DataGridViewRow = grdAccountTypes.SelectedRows(0)
                    If row.Index < rowCount - 1 Then
                        AccountTypesBindingSource.MoveNext()
                    End If
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Obviously you have to change AccountTypesBindingSource to the name of your binding source.


2) Otherwise we can control the CurrentRow by setting the CurrentCell property of the grid like this:

Try
            Dim rowCount As Integer = grdAccountTypes.Rows.Count
            If rowCount > 0 Then
                If grdAccountTypes.SelectedRows.Count > 0 Then
                    Dim row As DataGridViewRow = grdAccountTypes.SelectedRows(0)
                    If row.Index < rowCount - 1 Then
                        row = grdAccountTypes.Rows(row.Index + 1)
                        grdAccountTypes.CurrentCell = row.Cells(0)
                    End If
                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

In this case, however, the cell has to be visible. If row.Cells(0) is not visible, you should use row.Cells(1) or row.Cells(2) etc. Choose the index of whichever column you are sure is visible.

but i have noticed that if i would use the .MoveNext
i don't need to use all the rest
i can also use MovePrevious, Move.First, MoveLast.

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.