I have a VB.net datagridview in a project, unbound. In the RowValidated event, I determine whether I need to make a database write. If I do make a database write, I want to refill the grid, as I need the identity column of the newly added row to appear in the grid.
I use linq to sql to make the database writes, so, while still in the RowValidated event, I call dc.SubmitChanges, then I call my FillGrid (see code). As soon as I execute the line Me.dgvMKL.Rows.Clear(), I get the error:
Operation cannot be performed in this event handler.
Is there something I need to do to commit the grid or some such thing before trying to clear the rows? I tried invoking the EndEdit method in the RowValidated event, but that didn't help.
Private Sub FillGrid()
Dim dc As dcKittingdc = New dcKittingdc(Me._ConnString)
Dim MKLRecs As IQueryable(Of MKL) = From i In dc.MKLs
Where i.Job_Number = Me.tJob.Text _
AndAlso i.SS_Number = Me.tSS.Text _
AndAlso i.Install = Me.cbInstall.Text
For Each MKLRec As MKL In MKLRecs
Catch ex As Exception
Throw New Exception("At FillGrid/frmEditMKL: " + ex.Message)