Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Changing DataGridView.Enabled hangs application
 

Changing DataGridView.Enabled hangs application

The desktop application I'm developing has started hanging under exactly repeatable circumstances. The hang always occurs when I am disabling a DataGridView in the UI. There are three DataGridViews in the UI, but this problem has (thus far) only appeared when disabling one of them.

Here's what I know about the problem:
  • The problem only occurs when running the application from the desktop. The hang never appears when running the application from within Visual Studio.
  • The three DataGridViews in the application are bound to three different BindingSources.
  • The BindingSources all get their data from DataViews that are created off of DataTables in the same DataSet.
  • No UI objects or other BindingSources are bound to any object in that DataSet.
  • The program uses a BackgroundWorker to retrieve and update data that's in the DataSet. However, before launching a BackgroundWorker, the program calls SuspendBinding on all BindingSources, and it calls ResumeBinding in the RunWorkerCompleted event handler. All communication between the background method and the UI is done by calling Invoke in the form's event handler.
The way this program is used, a common occurrence in the UI is for the DataGridView in question to be disabled, a row to be added to its underlying DataTable, and then the control re-enabled. This problem only occurs immediately after the DataGridView contains enough rows that it will need to draw the scrollbar.

Here's what I've done to try to make this problem go away:
  • Added an assert that if the BackgroundWorker is busy, the DataGridView underlying BindingSource has binding suspended. This never fails: if the BackgroundWorker is updating the DataSet at the time the DataGridView is enabled or disabled, binding is suspended.
  • Turned off scrollbars in the offending DataGridView before enabling it, and then turning them back on. This was a workaround reported here: http://bytes.com/forum/thread595612.html, by someone who encountered what sounds like exactly the same problem I'm having.
  • If data-binding is not suspended, calling SuspendBinding on the BindingSource before changing Enabled, then calling ResumeBinding.
None of those has had any effect.

This is, as you might imagine from the level of effort I'm putting into solving it, a critical problem. What else should I be trying?
Robert Rossney  Wednesday, October 22, 2008 2:03 AM
Further research (a nice word for desperate trial-and-error) indicates that the problem seems to lie with the scroll bars. If I set them to ScrollBars.None on the offending DataGridView, the application stops hanging.

That's good, but I need the scroll bars. (Well, my users do.)
Robert Rossney  Wednesday, October 22, 2008 7:13 PM
Any ideas? Anybody?
Robert Rossney  Friday, October 31, 2008 5:44 PM
This is exactly what I have been facing with. I have a data retrieval operation done in a loop. I wanted to update the DataGrid to show the results, as and when a row is being retrieved. I used Threading to ensure that the UI does not hangs. However, the DataGrid hangs as soon as Scroll Bar ( Vertical ) appears ( when the rows exceed the DataGrid Size ).I even used BackGroundWorker class, to see if it helps, but with no luck.

I went by your suggestion and it has fixed. However, it does not look good as we need the scrollbar. Will try to do more research in this regard.
shriprasanna  Thursday, December 04, 2008 8:09 PM
I am having the exact same problem. Except I never suspend/resume binding of my data source in the DataGridView. My application hangs indefinitely if I add enough data to my datasource to require a scrollbar. Sure enough, if I set ScrollBars = ScrollBars.None before adding to the datasource it works fine. Except I don't get any scrollbars. It doesn't do this when run from the debugger because, for some reason, the data doesn't update in the DataGridView as it is being loaded into the datasource (I load data into my datasource in rows of 32). When deployed it has this issue though. I have worked on resolving this issue for sometime now and I haven't gotten any closser.

Does anyone have any ideas?


gongchengshi  Tuesday, January 27, 2009 3:07 AM
did anyone find the cause of this problem? I get nullreferenceexceptions when I try to set datagridview.readonly property to true unless I turn off the scrollbars first.
Vegeta4ss  Tuesday, March 24, 2009 4:37 PM
I have found that when using a BindingList as the data source for a DataGridView, the cause of the slowdown is that the BindingList fires a ListChanged event every time the list is changed. The DGV responds by updating/repainting itself every time this event is fired.

A solution that works for me is to create a class that inherits from BindingList , and then override the OnListChanged and OnAddingNew methods. During normal operation, these overrides simply call their respective base methods. However, when I want to perform a bulk load operation, I set my _loading variable to true so that the override methods do not call their base methods.

Here is my OnListChanged override method. My OnAddingNew is the same.
protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs e)<br />
{<br />
	if (_loading)<br />
	{<br />
		// Suppress ListChanged events during bulk load<br />
		return;<br />
	}<br />
	// Only fire ListChanged if we aren't loading<br />
	base.OnListChanged(e);<br />
}<br />

When the bulk load is complete, I set _loading to false, and call the OnListChanged method with the System.ComponentModel.ListChangedType.Reset argument so that the DGV will reload with the changed data
    OnListChanged(new System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.<strong>Reset</strong>
, 0));

Using this method has made a huge difference. Without _loading = false , it takes 13.8 seconds to load 9,821 records. With _loading = true , it takes 0.35 seconds. That is about 40 times faster !
Paul in Oz  Monday, October 05, 2009 12:28 AM

You can use google to search for other answers

Custom Search

More Threads

• Navigators addnewitem
• Date Range for Data Grid
• Update UI Thread
• DataBound Property Set is called twice
• Dataset Designer not Updating Code
• Creating an IBindingList from a Dictionary
• Tree View Control Expanding Problem
• datagridview column immage
• relational database-need the basics
• disable datagrid popup menu?