VB.net Add Records to bound DataGridView Mimic Return click

Hi

If I load a bound DataGridView with the right code I can manually add a record at the bottom and when
I click return it will add the record. Is there a way to mimic the Return click using VB.net code?

I tried the following but it did not commit the value. When I clicked in the last row everything dissapeared

       Dim oLastRowIndex As Long = Me.DataGridView1.RowCount - 1
          Me.DataGridView1.Rows(oLastRowIndex).Cells(2).Value = "x"
            Me.DataGridView1.Rows(oLastRowIndex).Cells(3).Value = "y"
            Me.DataGridView1.Rows(oLastRowIndex).Cells(4).Value = "z"
            Me.DataGridView1.Rows(oLastRowIndex).Cells(5).Value = "a"
            System.Threading.Thread.Sleep(10)
            SendKeys.SendWait("{ENTER}")

The following image shows how the code populates the last row. If doing this manually it stays there but with code
once I then click in there it vanishes
I also used the following code to commit the change but didn't help
  Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
        Try
            'This is used to make sure that a change is commited to a cell
            If DataGridView1.IsCurrentCellDirty Then
                DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        Catch ex As Exception

        End Try
    End Sub

1
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Try following


Dim oLastRowIndex as Integer =  DataGridView1.Rows.Add
Me.DataGridView1.Rows(oLastRowIndex).Cells(2).Value = "x"
Me.DataGridView1.Rows(oLastRowIndex).Cells(3).Value = "y"
Me.DataGridView1.Rows(oLastRowIndex).Cells(4).Value = "z"
Me.DataGridView1.Rows(oLastRowIndex).Cells(5).Value = "a"
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi

I get an error saying that rows cannot be added when the DataGridView is data bound
0
CodeCruiserCommented:
You need to add the row to your datatable then

dTable.Rows.Add(New Object() { Col1Value, Col2Value, Col3Value...})
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great answer. Like the way you think! Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.