Implementation method of comboBox control data binding in WinForm
- 2021-12-13 09:02:19
- OfStack
This paper describes the implementation method of comboBox control data binding in WinForm. Share it for your reference, as follows:
Here are three ways to bind comboBox, IList and Dictionary in generics, and DataTable in dataset
1. IList
Now let's create an List collection directly, and then bind it
IList<string> list = new List<string>();
list.Add("111111");
list.Add("222222");
list.Add("333333");
list.Add("444444");
comboBox1.DataSource = list;
After execution, we will find that the binding is successful, but we know that the binding for the drop-down box will have 1 value and 1 displayed content. At this time, we can create a class to encapsulate value and text into this class as the type of list
public class Info
{
public string Id { get; set; }
public string Name { get; set; }
}
private void bindCbox()
{
IList<Info> infoList = new List<Info>();
Info info1 = new Info() { Id="1",Name=" Zhang 3"};
Info info2 = new Info() { Id="2",Name=" Li 4"};
Info info3 = new Info() { Id = "3",Name = " Wang 5" };
infoList.Add(info1);
infoList.Add(info2);
infoList.Add(info3);
comboBox1.DataSource = infoList;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
}
At this time, we can directly get the value and display content
2. Dictionary
This is a bit special, can not be directly bound, you need to use the class BindingSource to complete the binding
Dictionary<int, string> kvDictonary = new Dictionary<int, string>();
kvDictonary.Add(1, "11111");
kvDictonary.Add(2, "22222");
kvDictonary.Add(3, "333333");
BindingSource bs = new BindingSource();
bs.DataSource = kvDictonary;
comboBox1.DataSource = bs;
comboBox1.ValueMember = "Key";
comboBox1.DisplayMember = "Value";
3. Data sets
This is quite common and very simple
// Dataset binding
private void BindCombox()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("id");
DataColumn dc2 = new DataColumn("name");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr1 = dt.NewRow();
dr1["id"] = "1";
dr1["name"] = "aaaaaa";
DataRow dr2 = dt.NewRow();
dr2["id"] = "2";
dr2["name"] = "bbbbbb";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
}
Note:
When we trigger the SelectedIndexChanged event of combox, we will execute it when loading the form, which I just started with charm, which leads to error-prone. We can take some measures to avoid executing it, such as defining a variable fig=false
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.fig)
{
string selectValue = this.cmbAddMember.SelectedValue.ToString();
rtbaddMember.SelectedText = selectValue;
}
}
Then you must want to execute it after loading the form, so we also need to set the value of fig to true after loading the form
private void SetAutoMessage_Load(object sender, EventArgs e)
{
loadCombox();
loadMessageTemplet();
fig= true;
}
For more readers interested in C # related content, please check the topics on this site: "WinForm Control Usage Summary", "C # Form Operation Skills Summary", "C # Data Structure and Algorithm Tutorial", "C # Common Control Usage Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"
I hope this article is helpful to everyone's C # programming.