Winform Combox 控件 绑定数据源时,触发SelectChange解决方案

   List lstinfo = new List { };
            lstinfo.Add(new ComboxInfo() { Name = "name1", Value = 1, Name3 = "name3-1" });
            lstinfo.Add(new ComboxInfo() { Name = "name2", Value = 2, Name3 = "name3-2" });

            comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;//删除事件
            comboBox1.DataSource = lstinfo;//默认,绑定数据源会触发SelectIndex事件
            comboBox1.DisplayMember = "Name3";//默认,绑定数据源会触发SelectIndex事件
            comboBox1.ValueMember = "Name";//如果和display不一样,U会触发
           
            comboBox1.SelectedIndex = 1;//设置默认值
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;//注册事件

你可能感兴趣的:(C#,.NET,使用技巧)