绑定源控制

时间:2016-12-15 14:08:11

标签: c# winforms bindingsource

我的表单中有一个绑定源代码控件。我在表单中使用绑定源current_changed事件来执行一些特殊任务,我面临的问题是,在form_Load事件中,我将列表作为此绑定源的数据源,并且多次调用current_changed事件。为什么会这样?

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<Employee> listEmployee = new List<Employee>();
        for (int i = 1; i <= 10; i++)
        {
            Employee emp = new Employee();
            emp.EmployeeName = "user" + i;
            emp.EmployeeAddress = "Address" + i;
            listEmployee.Add(emp);
        }
        bindingSource1.DataSource = listEmployee;
        dataGridView1.DataSource = bindingSource1;
    }

    private void bindingSource1_CurrentChanged(object sender, EventArgs e)
    {
        MessageBox.Show("Hai");
    }
}
public class Employee
{
    private string Name;
    private string Address;

    public string EmployeeName {
        get {return Name;}
        set { Name = value; }
    }

    public string EmployeeAddress
    {
        get { return Address; }
        set { Address = value; }
    }
}

1 个答案:

答案 0 :(得分:0)

对于我的特殊情况,我在页面加载时创建了一个标志,并且多次阻止了currentchanged事件的执行。我无法找到任何其他方法来解决它。