Search code examples
c#winformsbindingsource

Binding Source Control


I have a binding source control in my form. I utilize the binding source current_changed event in the form for doing some special tasks, the problem that I faces is, in the form_Load event I make a list as the datasource of this binding source and the current_changed event has been called multiple times. Why it's like that?

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

Solution

  • For my special case I created a flag on the page load and prevented the execution of currentchanged event more than once. I couldn't find any other way to solve it.