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() Try Me.dgvMKL.Rows.Clear() 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 Select i For Each MKLRec As MKL In MKLRecs Me.dgvMKL.Rows.Add(MKLRec.Job_Number, _ MKLRec.SS_Number, _ MKLRec.Install, _ MKLRec.Real_Part_Number, _ MKLRec.Qty.ToString(), _ MKLRec.uom, _ MKLRec.Active, _ MKLRec.User, _ MKLRec.DateTime.ToString(), _ MKLRec.Top_Collector, _ MKLRec.MKL_Id.ToString()) Next Catch ex As Exception Throw New Exception("At FillGrid/frmEditMKL: " + ex.Message) End Try End Sub