开发者

ComboBox.DataSource assignment taking ages

开发者 https://www.devze.com 2023-01-14 00:09 出处:网络
I have the following code: List<string> list = SomeFunction(); this.myComboBox.DataSource = list;

I have the following code:

List<string> list = SomeFunction();
this.myComboBox.DataSource = list;

For some reason, the assignment to DataSource is taking a surprising amount of time (about 1.4 seconds), when the list only contains 4 items.

Any idea why this is?

Edit:

SomeFunction() looks in several folders on disk for the existence of a particular .xml file, and if it exists, puts the containing folder name into the list which it returns.

Investigation:

I stuck a call to DateTime.Now around the DataSource assignment, and it comes back at ~1400ms each time. This leads me to believe that it is purely the DataSource assignment tha开发者_高级运维t causes it. However, I created a dummy List; Add()ing strings directly to it. This datasource executes in ~200ms. So perhaps it is not the DataSource assignment after all, and has something to do with SomeFunction. However, then I tried just inserting the strings one by one, and it executes in ~0ms.


It was in the SelectedIndexChanged event handler, where I was loading a file. It turns out that an assignment to the DataSource member will automatically change SelectedIndex (presumably back to 0, if it exists), whereas calling Items.Add will not do so.

In my case, it was a problem because I wanted to manually choose which item to load, and so it was loading twice - once automatically because of the event caused by the assignment (undesired) and once manually.


Try direct assign of your function to ComboBox.

eg: this.myComboBox.DataSource=SomeFunction();

0

精彩评论

暂无评论...
验证码 换一张
取 消