绑定源代码管理

Binding Source Control

我的表单中有绑定源代码管理。我利用表单中的绑定源 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; }
    }
}

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