Datagridview as dataentry facing issue

Malik Asad Mahmood 126 Reputation points
2020-12-02T02:37:15.043+00:00

Hi, Please I am using following code in vb.net in order to move datagridview cursor to move to next cell upon enter key, I am facing problem that when cell reached to last column upon enter its insert new row into datagridview before first row which is inserted by user I want that new row will be inserted as last row as ascending order and always new row insert after row which is inserted further refer to snapshot, I one issue I am facing on first column in datagridview I am calling child form whenever cell of first column in editmode its didnot return value to datagridview please help me

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![44128-mainsnap.jpg][1]  
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,607 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-12-03T07:36:08.313+00:00

    Hi @Malik Asad Mahmood ,

    I want that new row will be inserted as last row as ascending order and always new row insert after row which is inserted further

    I make a test based on your code and the following code works for me.

        Private dt As DataTable = New DataTable()  
        Private num As Integer = 0  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            DataGridView1.AllowUserToAddRows = False  
            dt.Columns.Add("c1")  
            dt.Columns.Add("c2")  
            dt.Rows.Add("A", 12)  
            dt.Rows.Add("B", 21)  
            DataGridView1.DataSource = dt  
        End Sub  
        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  
                        If DataGridView1.CurrentCell.RowIndex + 1 = DataGridView1.Rows.Count Then  
                            num += 1  
                            Dim row As DataRow = dt.NewRow()  
                            row(0) = num  
                            row(1) = num  
                            dt.Rows.Add(row)  
                            DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex + 1).Cells(0)  
                            Return True  
                        Else  
                            DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex + 1).Cells(0)  
                            Return True  
                        End If  
      
                    Catch ex As ArgumentOutOfRangeException  
                        Return True  
                    End Try  
                End If  
            End If      
            Return MyBase.ProcessCmdKey(msg, keyData)  
        End Function  
    

    Result of my test.
    44649-gif1.gif

    Could you provide more related code about your second question? It will help us make a test.

    Best Regards,
    Xingyu Zhao
    *
    If the answer 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 comments No comments