如何在使用的WinForms EF双向,数据绑定? [英] How to make two way-databinding using EF in winforms?

查看:279
本文介绍了如何在使用的WinForms EF双向,数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编码很新。现在我要告诉你关于我的现状和在那里我想去的地方。希望你会有所帮助。

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.

我使用EF5.0数据库第一种方式,我希望能够使用GridView的上市和更新数据库值。

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();

这代码列出我的完美数据。然而这是错误的或没有足够满足我的需求。因为我希望能够编辑有权在GridView的我的数据。请告诉我什么是错的,我该怎么办。在一些文件,将达到我的目标点我的愿望将是巨大的至少联系。
THX。

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.

推荐答案

如果您绑定到查询结果您只使用单向数据绑定。你需要有 IBindingList的来获取双向数据绑定。试试这个:

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();

如果它不工作,尝试使用的BindingSource

If it doesn't work try to use BindingSource:

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

这篇关于如何在使用的WinForms EF双向,数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆