Avatar of g_johnson
g_johnson
Flag for United States of America asked on

cannot clear datagridview rows, VB.Net

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.

Thanks,

 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

Open in new window

Visual Basic.NETLINQ Query

Avatar of undefined
Last Comment
g_johnson

8/22/2022 - Mon
Éric Moreau

have you tried dgvMKL.DataSource = Nothing
g_johnson

ASKER
The grid is never bound, but I tried this anyhow.  No luck.
ASKER CERTIFIED SOLUTION
g_johnson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
g_johnson

ASKER
Solves the problem, no other viable solutions offered
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23