« SHDocVw, Microsoft Web Browser ActiveX Control | Main | Understanding Windows Forms layout and Dock behavior. »

DataGrid, ArrayList, Threads

Converted the JobSearchDesignerOnly project to use a second Thread for the Spider. Turned out to be easier than expected. The only trick was in the CrawlResultsEventHandler which returns a results summary of each page crawled. With the Spider running on its own thread, the first event handler would violate the Windows.Forms STA threading model if it tried to directly update the GUI, so the event is redirected by a BeginInvoke call to a second delegate which runs on the controls thread. Worked like a charm. There’s also some complexity around making sure the thread gets cleaned up and only started once the GUI is up. See JobSearchControls.SpiderPanel.cs.

ArrayLists of objects with public properties only sort-of works with DataGrids. I could find no way of having the grid update when results where added to the ArrayList. Switched to DataTable and DataView and everything works like a charm.

Put the GUI for the Spider results in a UserControl with a WebBrowser and DataGrid on it. This is definitely the way to build hunks of UI.

KzDataGrid derives from DataGrid and adds support for automatic column width adjustments, but only when using DataTableStyles.

Finally figured out how to inhibit the “AddNew” row in a DataGrid bound to a DataSet:

((DataView)((CurrencyManager) jobDataGrid.BindingContext[dataSet, "Job"]).List).AllowNew = false;

The List member returns the object which implements the IList interface, which in the case of a DataSet will be a DataView. The BindingContext of the data for the grid will be handled by a CurrencyManager, the alternative PropertyManager only handles single values.

Comments

If you'd like ArrayList as DataSource for your DataGrid, you can use CurrencyManager to refresh the grid. That will avoid the IndexOutOfRangeException you get if you just run Refresh(). Do this(C#):
1.When initializing the grid:
myDataGrid.DataSource = this.myArrayList;
2.After you have modified the arraylist (deleted/added items):
CurrencyManager cm = (CurrencyManager)myDataGrid.BindingContext[myArrayList];
cm.Refresh();

.. which I found out after four hours of googling :-/
For VB example, check The Code Project - "DataGrid101":
http://www.codeproject.com/vb/net/grid101.asp

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)