简体   繁体   中英

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:

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. 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.

May someone help me?

1.) Create a Binding Source

2.) Set the Datasource for this object to your Dataset Table.

3.) Set The datasource for your DatagridView as the Binding source Object.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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