views:

266

answers:

2

I have a ComboBox (Windows Forms) that is bound to a List. It is created at design time. When the List contents are changed my code calls a function to refresh the data binding. This works fine for .NET 3.5:

BindingData.SuspendBinding();
DataSource = null;
DataSource = BindingData;
BindingData.ResumeBinding();

I have switched to .NET 4.0 and it has stopped working. Specifically after stepping through this code the VS debugger shows BindingData.DataSource refers to a list with 127 items, but the ComboBox Items property contains zero items.

See this SO question along a similar theme: ComboBox Items Count Doesn't Match DataSource.

I have tried everything I can think of. Currently my code looks like the following and still doesn't work:

BindingData.SuspendBinding();
DataSource = null;
DataSource = BindingData;
BindingData.ResumeBinding();
BindingContext Dummy = this.BindingContext;
Invalidate();
PerformLayout();

I tried switching from List to BindingList and that didn't help. I had to switch from .NET 3.5 to .NET 4.0 against my will so this is pretty frustrating. I'm sure there is a specific sequence that works. Any ideas?

This is how I am attaching the data source to the ComboBox:

private BindingSource BindingData = new BindingSource();

BindingData.DataSource = Nodes;
DataSource = BindingData;

thanks, Andy

A: 

I solved it. I guess at some point I made what I thought was a minor change but actually wasn't. This code was moved from being called when the ComboBox is being displayed to when it was being created. It didn't yet have a handle and so the data binding cannot be refreshed.

I added another refresh of the databinding again in a ComboBox.HandleCreated event and it works.

thanks, Andy

Andy
A: 

Why you're suspending and resuming the BindingSource? If you just change your DataSource there will be no performance pitfalls.

Timur Zanagar