ComboBox Data Binding Update in C # and .NET 4.0

I have a ComboBox (Windows Forms) related to a List. It is created during development. When the contents of the list change, my code calls a function to update the data binding. This works fine for .NET 3.5 :

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

I switched to .NET 4.0 and it stops working . In particular, after passing this code, the VS debugger shows BindingData.DataSource refers to a list of 127 items, but the ComboBox Items property contains zero items.

See this SO question on a similar topic: The number of ComboBox elements does not match the DataSource .

I tried everything I could think of. Currently my code is as follows and still not working:

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

List BindingList, . .NET 3.5 .NET 4.0 , . , , . ?

ComboBox:

private BindingSource BindingData = new BindingSource();

BindingData.DataSource = Nodes;
DataSource = BindingData;

,

+3
3

. , - , , , , . , ComboBox . , .

ComboBox.HandleCreated .

,

+1

BindingSource? DataSource, .

0

: Windows Forms ComboBox ListBox ComboBox DisplayMember:

//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 length instead of the item itself
comboBox.DisplayMember = "";      //reset -> the ComboBox calls each List item ToString
                                  //member

"" String. , . String Chars, . , reset DisplayMember comboBox.DisplayMember = "", ComboBox (a String) ToString method = > .

List entries other than strings can be processed by the ComboBox properties DisplayMemberand ValueMember(they also apply to other controls): DisplayMember and ValueMember

0
source

Source: https://habr.com/ru/post/1765149/


All Articles