Solved

cannot clear datagridview rows, VB.Net

Posted on 2014-01-09
4
950 Views
Last Modified: 2016-02-10
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

0
Comment
Question by:g_johnson
  • 3
4 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39768212
have you tried dgvMKL.DataSource = Nothing
0
 
LVL 4

Author Comment

by:g_johnson
ID: 39768502
The grid is never bound, but I tried this anyhow.  No luck.
0
 
LVL 4

Accepted Solution

by:
g_johnson earned 0 total points
ID: 39769229
I solved this using information I found here:

http://stackoverflow.com/questions/4960232/operation-cannot-be-performed-in-this-event-handler


I used a timer.
0
 
LVL 4

Author Closing Comment

by:g_johnson
ID: 39778779
Solves the problem, no other viable solutions offered
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now