[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


VB.net Add Records to bound DataGridView Mimic Return click

Posted on 2014-07-17
Medium Priority
Last Modified: 2014-07-18

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"

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
            'This is used to make sure that a change is commited to a cell
            If DataGridView1.IsCurrentCellDirty Then
            End If
        Catch ex As Exception

        End Try
    End Sub

Question by:Murray Brown
  • 2
  • 2
LVL 83

Expert Comment

ID: 40202342
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"

Author Comment

by:Murray Brown
ID: 40204005

I get an error saying that rows cannot be added when the DataGridView is data bound
LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 40204179
You need to add the row to your datatable then

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

Author Closing Comment

by:Murray Brown
ID: 40204223
Great answer. Like the way you think! Thanks

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question