简体   繁体   English

取消绑定并再次绑定datagridview C#和Entity Framework

[英]Unbind and bind again a datagridview C# & Entity Framework

I updated the data in the datagridview, but I'm not able to see the changes until I close and open the program, I tried this codes: 我更新了datagridview中的数据,但在关闭并打开程序之前我无法看到更改,我尝试了以下代码:

datagridview1.DataSourse = null; //to clear the datagridview
datagridview1.DataSource = dcontext.Empleados.ToList(); //to fill the datagridview again

But doing this I'm not able to see the changes that were made, I see the same datagridview1 I had in the beginning. 但是这样做我无法看到所做的更改,我看到我在开始时使用的datagridview1。 I also tried a code that adds 我还尝试了一个添加的代码

something.Rows.Clear() //I dont remember the exact way it was written.

I have read that I need to unbind the datagridview and fill it(bind it) again. 我已经读过我需要取消绑定datagridview并再次填充它(绑定它)。

May someone help me? 愿有人帮帮我吗?

1.) Create a Binding Source 1.)创建绑定源

2.) Set the Datasource for this object to your Dataset Table. 2.)将此对象的数据源设置为数据集表。

3.) Set The datasource for your DatagridView as the Binding source Object. 3.)将DatagridView的数据源设置为绑定源对象。

Dataset ds = new Dataset();
BindingSource bs = new BindingSource()
bs.Datasource = ds.Table[0];
datagridview1.Datasource = bs;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM