Search code examples
c#data-bindingdatagridviewbindingsource

Data Binding from Binding source


In my C# WinForm, I have a binding source and data grid view. On the Form_Load event;

_bsCompany = new BindingSource();
_listOfCompany = CompanyService.GetListCompany();//Gets a listofCompany
_bsCompany.DataSource = _listOfCompany;

dgvCompany.DataSource = _bsCompany;

And I have binded it to text box;

txtCompanyID.DataBindings.Add("Text", _bsCompany, "CompanyID");

This is working. When I click any record in the gridview the CompanyID is displayed in the textbox.

My question is , If I delete one of the company from the list the binding is stuck and textbox is not displaying the selected value from the datagridview. The value for the deleted item is in the textbox after delete;

Am I doing wrong? Am I need to clear and rebind the textbox?

EDIT: I have cleared and rebind like this, but same problem.

txtCompanyID.DataBindings.Clear();
txtCompanyID.DataBindings.Add("Text", _bsCompany, "CompanyID");

Solution

  • OK I did it like this; (Forgot to update the question with how I solved this)

    _listOfCompany.Remove(company);
    _bsCompany.ResetBindings(true);
    

    This will reset the bindings between controls and data source