C#_WinForm的ComboBox与数据绑定详解(关于Text属性和下拉列表)

首先,ComboBox一共需要绑定两套数据源。第一套是Text属性,当Text被更改后,Text值被推送给数据源;或者是,当数据源发生值改变,则值被现实到ComboBox的Text里。

代码:

DataTable table = .....;
BindingSource bindingSource = new BindingSource();
bindingSouce.DataSource = table;
comboBox.DataBindings.Add("Text", bindingSource, "需要绑定的表的字段的名称");


其次,ComboBox的下拉列表,是另一套数据源,需要重新绑定。上面的Text绑定与下拉列表的绑定,不会冲突,可以同时进行。

代码:

DataTable table_list = .....;
BindingSource bindingSource_list = new BindingSource();
bindingSouce_list.DataSource = table_list;
comboBox.DataBindings.Add("Text", bindingSource_list, "需要绑定的存储着下拉列表数据的表的字段的名称");
原文地址:https://www.cnblogs.com/xxxteam/p/2890548.html