.net c#来自绑定源的数据绑定

Data Binding from Binding source
2021-09-14
  •  译文(汉语)
  •  原文(英语)

在我的C#WinForm中,我有一个绑定源和数据网格视图.在Form_Load事件上;

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

dgvCompany.DataSource = _bsCompany;

而且我已经将其绑定到文本框;

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

这正在工作.当我单击gridview中的任何记录时,公司ID将显示在文本框中.

我的问题是,如果我从列表中删除公司之一,则绑定被卡住,并且文本框未显示从datagridview中选择的值.删除项目的值在删除后的文本框中;

我做错了吗?我需要清除并重新绑定文本框吗?

编辑:我已经清除并像这样重新绑定,但是同样的问题.

txtCompanyID.DataBindings.Clear();
txtCompanyID.DataBindings.Add("Text", _bsCompany, "CompanyID");
速聊1:
尝试一下,看看是否会发生.
解决过程1

试试这个吧.



    txtCompanyID.DataBindings.Add("Text", dgvCompany.DataSource, "CompanyID");

解决过程2

好吧,我是这样做的;(忘了用我如何解决这个问题来更新问题)

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

这将重置控件和数据源之间的绑定

解决过程3

你可以试试看

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

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");
Talk1:
Try it and see want will happen.
Solutions1

Try this instead.



    txtCompanyID.DataBindings.Add("Text", dgvCompany.DataSource, "CompanyID");

Solutions2

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

Solutions3

you may try this instead

txtCompanyID.DataBindings.Add(new Binding("Text", _bsCompany, "CompanyID"));
转载于:https://stackoverflow.com/questions/15611787/data-binding-from-binding-source

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入