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

Losing a row in a DataGridView when hitting the Enter key?

$
0
0
For some reason when I add a row of data to my DataGridView and on the second to last row, I hit the enter key, the row disappears. I was capturing the keystroke in the KeyDown event but I took that out and it's still doing it. Below are all the events I'm using for my DGV.

Code:

    Private Sub dgvTrans_CellMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvTrans.CellMouseClick
        Try
            Select Case e.ColumnIndex
                Case 8
                    bsTrans.AddNew()
                    bsTrans.MoveLast()
                    dgvTrans.CurrentCell = dgvTrans.Rows(e.RowIndex + 1).Cells(3)
            End Select

        Catch ex As Exception
            Dim strErr As String = "frmTransactions/dgvTrans_CellMouseClick() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgvTrans_CellLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTrans.CellLeave
        Try
            If Not blnIsLoading Then
                Select Case e.ColumnIndex
                    Case 3  'After leaving the Account Column autofill the Date column with current date
                        dgvTrans.Rows(e.RowIndex).Cells(4).Value = Now.Date
                End Select
            End If

        Catch ex As Exception
            Dim strErr As String = "frmTransactions/dgvTrans_CellLeave() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgvTrans_DataError(sender As Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgvTrans.DataError
        Try

        Catch ex As Exception
            Dim strErr As String = "frmTransactions/dgvTrans_DataError() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgvTrans_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgvTrans.KeyDown
        Try
            Dim iRow As DataGridViewRow = dgvTrans.CurrentRow

            Select Case e.KeyCode
                Case 46    'Delete Key was pressed
                    bsTrans.RemoveAt(iRow.Index)
                Case 120    'F9 Key was pressed
                    bsTrans.AddNew()
                    bsTrans.MoveLast()
                    dgvTrans.CurrentCell = dgvTrans.Rows(iRow.Index + 1).Cells(3)
            End Select

        Catch ex As Exception
            Dim strErr As String = "frmTransactions/dgvTrans_KeyDown() - " & ex.Message
            MessageBox.Show(strErr, "User Notification", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


Thanks,

Viewing all articles
Browse latest Browse all 27509

Trending Articles



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