Solved

VB.Net DataGridView - RowsAdded Fired Twice

Posted on 2013-11-25
6
1,187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data organization issue 7 57
Include MS Database in Winforms Project using Intellilock 5 34
Import a excel sheet in a grid 2 45
Need a quick overview of Selenium testing 2 36
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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