Solved Add Records to bound DataGridView Mimic Return click

Posted on 2014-07-17
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 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:murbro
    LVL 83

    Expert Comment

    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


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

    Accepted Solution

    You need to add the row to your datatable then

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

    Author Closing Comment

    Great answer. Like the way you think! Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now