How to make two way-databinding using EF in winforms?

我只是一个虾纸丫 提交于 2019-12-19 19:53:32

问题


I am very new at coding. Now I am going to tell you about my current situation and where I wanna go. Hope you will help.

I am using EF5.0 database first approach and I want to be able to use gridviews for listing and updating the database values.

Currently I am binding my data like this:

  pehlivan_kabametrajEntities ctx = new pehlivan_kabametrajEntities();
        var result = from k in ctx.Kolons
                     select k;
        dataGridView1.DataSource = result.ToList();

This code lists my data perfectly. However it is wrong or not enough for my needs. Because I want to be able to edit my data right on the gridview. Please tell me what is wrong and what should I do. At least links of some documents that will reach me to the destination point I desire would be great. Thx.


回答1:


If you bind to query result you are using only one-way data binding. You need to have IBindingList to get two way data binding. Try this:

 ctx.Kolons.Load();
 dataGridView1.DataSource = ctx.Kolons.Local.ToBindingList();

If it doesn't work try to use BindingSource:

 ctx.Kolons.Load();
 gridBindingSource.DataSource = ctx.Kolons.Local.ToBindingList();
 dataGridView1.DataSource = gridSource; 


来源:https://stackoverflow.com/questions/12736958/how-to-make-two-way-databinding-using-ef-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!