English 中文(简体)
C# binding a combobox to a growing list
原标题:

WinForms, .NET 3.5

I have a ComboBox that needs to display a growing list of items -- there is a long running process fetching items, and I want to be able to display items to the user as they build up over time.

Setting the DatSource property to a naive Array/List doesn t work since subsequent additions are not registered by the ComboBox.

Now I can set the DatSource to a BindingList -- but it seems that this becomes tragically slow. When I throttle additions things go fine; but I m adding maybe a thousand items every couple of seconds, and this rate just hangs the form.

I m entirely willing to believe this is an inherent shortcoming of binding directly to a BindingList, but would like to be better understand what is going on. The MSDN articles on DatSource, BindingList and BindingSource discuss concepts like and BindingContexts, CurrencyManagers but don t help me understand the specific events, redraws and what-not that are being fired that might cause this slow down.

I suspect the BindingSource registers an item-add event to the ComboBox, which then goes through a series of Item-additions, DropDown-checks and invalidation. But MSDN browsing and Googling hasn t helped me answer this.

Can anyone point me in the right direction?


Clarification

I m aware of the design problems, and ways to work around this problem. So I m not really looking for a solution -- but I do want to understand the deeper mechanics of what is going on.

最佳回答

The default control is not made for such operations, since all entries are cached on the control as far as I know.

You need to use a control which properly supports a "virtual" mode, which means that it only loads the few items to be displayed currently from the list.

问题回答

You can try wrapping add operations in ComboBox.BeginUpdate() and ComboBox.EndUpdate(), but for this to have much help you may want to add batches of items. Another option would be to not use data binding and just use the ComboBox.Items collections. Not as fancy, but it should help.

I had a rough time displaying large amounts of data using the standard controls as well.

I will try to remember to post a link here to my code once I get it online, but to get around the issue I attached a scroll bar to a panel and used the value of the scroll to calculate what data should be visible in the panel.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签