Solved

VB.Net DataGridView - RowsAdded Fired Twice

Posted on 2013-11-25
6
1,140 Views
Last Modified: 2013-12-02
Hopefully I am just missing something obvious here, but for the life of me I cannot find anything.

I have a form on which there is a DataGridView that is bound directly to rows in a DataSet. All of this has been set up using the designer. I do not touch the binding related properties anywhere in my code.

The DataGridView consists of bound fields and unbound fields, the latter of which I calculate and populate in the RowsAdded() event (e.g. there is an invisible data bound field that is a number of seconds, and an unbound visible field that is that value formatted into HH:MM:SS).

Anyhow, the issue I have is that the RowsAdded() event seems to be firing 4 times, the first time with e.RowIndex = 0 and e.RowCount = 1, the second with e.RowIndex = 1 and e.RowCount = 88 (which I was expecting), but it then fires a third and fourth time with exactly the same sequence of parameters.

I do a fair bit (not excessive) of formatting in the event (cell background colouring, value formatting etc), and the fact that it is all being executed twice is obviously going to unnecessarily slow my application down.

I have looked everywhere and cannot fathom out why it would be being fired twice - the binding is done in design and never changes, and the Fill() is only called the once.

Any clues as to what I might have done wrong ?

Thanks,
Matt
0
Comment
Question by:matthewgreen
  • 5
6 Comments
 

Author Comment

by:matthewgreen
ID: 39675166
A little more information. In total I have 3 DataGridView controls on the screen. Two are bound to data in the Designer, one is unbound and rows are programmatically loaded.

The two DGV's that are bound have the double firing of the RowsAdded() event, the one that I programmatically control fires just the once.

Matt.
0
 

Author Comment

by:matthewgreen
ID: 39675405
To add further to my confusion, I have a "Refresh" button on the frm that causes a data refresh for the grid. This calls the exact same code that is called at application startup (I have a RefreshGrid() routine that is called in Form_Load() and Button_Click() events). On the button click the RowsAdded() event fires just the twice.

What can possibly be the difference between Form_Load() calling the routine and Button_Click() ?? They are both running the same code.

I have checked and the Foam_Load() is only fired once, and there are only 3 references to the RefreshGrid() routine in the entire solution - 1 in Form_Load(), 1 in Button_Click, and the declaration.

It appears to be an issue with Data Binding to a grid at startup - everything seems to be done twice. Clearly if I take control myself (as I have done for one grid) and manually load the data then all is well, but if left to its own devices having bound to the data source at design time, it all seems to go wrong.

This is driving me crazy!!

Matt
0
 
LVL 40
ID: 39675836
In my opinion, the events in the DataGridView are a complete mess, so finding the cause of such a problem can be very hard.

I encountered the same one a few times, and ended up resorting to an inelegant solution, but it worked in my case.

I use a Static Boolean variable to make the event execute my code only once every 2 calls. A Static variable is a local variable that is not destroyed and keeps it value between calls.
Static flag As Boolean

If flag Then
    flag=False
    'Do nothing else
Else
    'Do your job
    flag=True
End If

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:matthewgreen
ID: 39675917
Hmm, not sure that solution would always be reliable. In my case the first two calls are needed, the next two calls are seemingly 100% redundant, and then every subsequent call (either from a button click or a timer) will be needed. It seems only that the initial loading causes an issue.

I could control that via the flag idea, but what makes me uncomfortable here is that documentation of the RowsAdded() event seems to imply that the number of calls may vary, hence the e.RowIndex and e.RowCount values being available. As such, any assumption that there will always be two calls to complete the load could come back to bite me.

It seems that if you control the loading of the rows from a data source to the DGV yourself (making the DGV completely unbound) the the events seem to fire okay (as evidenced by one of my grids that I control myself already).

I might try this tomorrow when back at work, to see if there is reasonable performance when I am programmatically doing all the work myself. Just seems a bit mad that the events of the DGV are so unpredictable on initial load.

Matt
0
 

Accepted Solution

by:
matthewgreen earned 0 total points
ID: 39680633
Okay, latest state of play.

I have removed all code out of any event related to DataBinding (DataBindingComplete, RowsAdded etc) and have left only event code that reacts to user actions. Anything related to the formatting of the grid, calculation and loading of unbound data is now under my control.

I have a new routine that does this, and the new sequence is:

JobData.ClearBeforeFill = True
JobData.Fill()
FormatJobData()

The FormatJobData() routine just cycles the rows in the grid:

For Each JobRow As DataGridViewRow In JobDataGrid.Rows
:
:
Next JobRow

.... and does the necessaries with each row.

All in all not much change in the coding, just the location and invocation. Result is the same, time to run reduced from 29 seconds when using the Grid Events to 1 second now.

Results speak for themselves. I am off to alter my other grid now, and am going to steer well clear of letting the Grid Events do much of anything in the future.

Matt
0
 

Author Closing Comment

by:matthewgreen
ID: 39689433
It was a rework undertaken myself that resulted in the issue being resolved, if you can call "avoiding the point of the issue" a solution. All I have done is avoid using the Grid Events that were causing the issue.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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