Selected the new row after added in a datagridview (using a datasource)

  • Thread starter Thread starter Padpanix
  • Start date Start date
P

Padpanix

Guest
Hello,

I use Vb Express 2010 - .NET Framework 4 Client Profile, OS : Windows 10 version 1909)!

Here is the very simplified code. (I may have made clerical mistakes by simplifying, but that's the idea).


Dim BDS_DT as bindingsource = nothing
Dim DT as datatable = loadDT(FileName) ' Load DataTable
BDS_DT = New BindingSource(DT, Nothing)
DataGridView1.DataSource = BDS_DT
Bds_DT.filter = ....

' I add new row

' -------------
Dim DRNew as datarow = dt.newrow
DRNew("ID") = "abcdef"
DRNew("...") = ....
Dt.rows.add(Dr)
DTRNew.AcceptChanges


For selected, I tried to add the following line, just after but it doesn't work!

DGVRowSelectID("abcdef")

with
Public Sub DGVRowSelectID(ByVal ID As String)
If Not IsNothing(DBS) Then
DBS.EndEdit()
Dim Index As Integer = DBS.Find("ID", ID)
If Index >= 0 Then DBS.Position = Index
End If
End sub

I read that you had to use the DatabindingComplet of the datagridview but it does not work.
It looks like the line is added after performing the databindingComplet.

Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
DGVRowSelectID("abcdef")
End sub

I also tried

Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete

Me.DataGridView1.BeginInvoke(CType(Sub()
DGVRowSelectID("abcdef")
End Sub, MethodInvoker))

End sub


Thanks for your help





Steeve

Continue reading...
 
Back
Top