Try to add this code, it can make press enter to get a response that meets your requirements when the DataGridView is in EditMode mode.
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Enter Then
If DataGridView1.CurrentCell.ColumnIndex < DataGridView1.ColumnCount - 1 Then
DataGridView1.CurrentCell =
DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(DataGridView1.CurrentCell.ColumnIndex + 1)
Return True
Else
Try
DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex + 1).Cells(0)
Return True
Catch ex As ArgumentOutOfRangeException
Return True
End Try
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Update:
If you want to add a blank row in the middle of the datagridview for adding data. You can use DataTable.NewRow Method.
Get the current number of rows in the CellClick event, and add a blank row to the next row.
At the same time, in order to prevent adding too many blank rows, I also added a verification to the current datatable. If there are already blank rows, you cannot continue to add new blank rows.
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer = dt.AsEnumerable().Where(Function(row) IsDBNull(row("ID")) OrElse IsDBNull(row("Name")) OrElse IsDBNull(row("Score"))).Count()
If i = 0 Then
Dim newRow As DataRow = dt.NewRow()
dt.Rows.InsertAt(newRow, e.RowIndex + 1)
End If
End Sub
In fact, I feel that if you add a new button column to the datagridview, it might be easier to add a new row when the button is clicked.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.