简体   繁体   中英

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

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.

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; 

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