开发者

Refreshing ComboBox Data Binding in C# and .NET 4.0

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


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


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


According to How to: Bind a Windows Forms ComboBox or ListBox Control to Data you can use the ComboBox's DisplayMember property:

//Sample for C++ .NET:
List<String^>^ options = gcnew List<String^>();
options->Add("Option 1");
options->Add("Option 2");

comboBox.DataSource = options;  
comboBox.DisplayMember = "Length";//this causes an DataSource update but the ComboBox would
                                  //show an item's length instead of the item itself
comboBox.DisplayMember = "";      //reset -> the ComboBox calls each List item's ToString
                                  //member

"Length" refers to a public property of the String class. Better would be a property that refers directly to the string's characters. The only remaining public property of String is Chars but I couldn't make it work. So we reset DisplayMember by comboBox.DisplayMember = "", causing the ComboBox to call each List item's (a String) ToString method => problem solved.

Other List entries than Strings can be handled by the ComboBox's properties DisplayMember and ValueMember (they also apply to other controls): DisplayMember & ValueMember

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜