Datagridview insert new Row after First

Malik Asad Mahmood 126 Reputation points
2020-11-30T18:26:09.073+00:00

Hi,

Please I need help , as I am using datagridview when user press enter key its move to next cell instead of new to new row when its reach to last cell

upon user enter key on last cell or row its move to new row, the following code is insert new row but its inserting rows as first row, I want new inserted row will be added as second row upon row filled by user third new row will be inserted and so on

my code as followed.

Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    If e.KeyCode = Keys.Enter Then


        Dim c As DataGridViewCell = DataGridView1.CurrentCell
        If c.ColumnIndex = DataGridView1.ColumnCount - 1 And c.RowIndex < DataGridView1.RowCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex + 1).Cells(0)

        ElseIf c.ColumnIndex < DataGridView1.ColumnCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex).Cells(c.ColumnIndex + 1)

        ElseIf c.ColumnIndex = DataGridView1.ColumnCount - 1 Then


            DataGridView1.Rows.Add(c.RowIndex + 1)
        End If
        e.Handled = True
    End If
End Sub

Private Sub ec_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles ec.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then

        DataGridView1.EndEdit()
        Dim c As DataGridViewCell = DataGridView1.CurrentCell
        If c.ColumnIndex = DataGridView1.ColumnCount - 1 And c.RowIndex < DataGridView1.RowCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex + 1).Cells(0)

        ElseIf c.ColumnIndex < DataGridView1.ColumnCount - 1 Then
            DataGridView1.CurrentCell = DataGridView1.Rows(c.RowIndex).Cells(c.ColumnIndex + 1)

        End If
    End If
End Sub
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,457 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points
    2020-12-01T02:43:33.323+00:00

    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.


0 additional answers

Sort by: Most helpful