Murray Brown
asked on
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(oLas tRowIndex) .Cells(2). Value = "x"
Me.DataGridView1.Rows(oLas tRowIndex) .Cells(3). Value = "y"
Me.DataGridView1.Rows(oLas tRowIndex) .Cells(4). Value = "z"
Me.DataGridView1.Rows(oLas tRowIndex) .Cells(5). Value = "a"
System.Threading.Thread.Sl eep(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_CurrentCellD irtyStateC hanged(sen der As Object, e As System.EventArgs) Handles DataGridView1.CurrentCellD irtyStateC hanged
Try
'This is used to make sure that a change is commited to a cell
If DataGridView1.IsCurrentCel lDirty Then
DataGridView1.CommitEdit(D ataGridVie wDataError Contexts.C ommit)
End If
Catch ex As Exception
End Try
End Sub
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(oLas
Me.DataGridView1.Rows(oLas
Me.DataGridView1.Rows(oLas
Me.DataGridView1.Rows(oLas
System.Threading.Thread.Sl
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_CurrentCellD
Try
'This is used to make sure that a change is commited to a cell
If DataGridView1.IsCurrentCel
DataGridView1.CommitEdit(D
End If
Catch ex As Exception
End Try
End Sub
ASKER
Hi
I get an error saying that rows cannot be added when the DataGridView is data bound
I get an error saying that rows cannot be added when the DataGridView is data bound
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great answer. Like the way you think! Thanks
Dim oLastRowIndex as Integer = DataGridView1.Rows.Add
Me.DataGridView1.Rows(oLas
Me.DataGridView1.Rows(oLas
Me.DataGridView1.Rows(oLas
Me.DataGridView1.Rows(oLas