Solved

cannot clear datagridview rows, VB.Net

Posted on 2014-01-09
4
1,019 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 70

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

11 Experts available now in Live!

Get 1:1 Help Now