How to make two way-databinding using EF in winfor

2019-02-22 23:41发布

问题:

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;