zoukankan      html  css  js  c++  java
  • [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定

    一、IList

          现在我们直接创建一个List集合,然后绑定

     IList<string> list = new List<string>();
     list.Add("111111");
     list.Add("222222");
     list.Add("333333");
     list.Add("444444");
     comboBox1.DataSource = list;

     执行后,我们会发现绑定成功,但是 我们知道一般对于下拉框的绑定都会有一个值,一个显示的内容,这个时候我们可以创建一个类,把value和text都封装到这个类,作为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="张三"};
                Info info2 = new Info() { Id="2",Name="李四"};
                Info info3 = new Info() { Id = "3",Name = "王五" };
                infoList.Add(info1);
                infoList.Add(info2);
                infoList.Add(info3);
                comboBox1.DataSource = infoList;
                comboBox1.ValueMember = "Id";
                comboBox1.DisplayMember = "Name";
            }

    二、Dictionary

         这个有点特殊,不能直接绑定,需要借助类BindingSource才可以完成绑定

    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";

    三、数据集

         这个比较常见,很简单

    //数据集绑定
            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";
            }

    注意:

    当我们触发combox的SelectedIndexChanged的事件后,我们在加载窗体的时候就会执行,这点我刚开始也和魅惑,导致容易出错,这点我们可以采取一些方法避免执行,比如可以定义一个变量fig=false

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
       {
           if(this.fig)
           {
               string selectValue = this.cmbAddMember.SelectedValue.ToString();
     
               rtbaddMember.SelectedText = selectValue;
           }
       }

    那么肯定想在加载窗体后,执行了,所以在加载窗体后我们还要把fig的值设为true

    private void SetAutoMessage_Load(object sender, EventArgs e)
            {
                loadCombox();
                loadMessageTemplet();
                fig= true;
            }

    转自:https://www.cnblogs.com/masonlu/p/9300241.html

  • 相关阅读:
    LeetCode Find Duplicate File in System
    LeetCode 681. Next Closest Time
    LeetCode 678. Valid Parenthesis String
    LeetCode 616. Add Bold Tag in String
    LeetCode 639. Decode Ways II
    LeetCode 536. Construct Binary Tree from String
    LeetCode 539. Minimum Time Difference
    LeetCode 635. Design Log Storage System
    LeetCode Split Concatenated Strings
    LeetCode 696. Count Binary Substrings
  • 原文地址:https://www.cnblogs.com/wrld/p/10366763.html
Copyright © 2011-2022 走看看