DataGridView的使用LINQ到实体匿名类型 [英] Datagridview with linq to entities anonymous type

查看:180
本文介绍了DataGridView的使用LINQ到实体匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力理解如何更新从已经充满了使用LINQ to实体LINQ查询一个DataGridView的SQL数据库。

I am struggling to understand how to update an SQL database from a datagridview that has been filled with a linq query using linq to entities.

我拉从数据三个表;项目,任务和linkMissionItem用下面的查询:

I pull in data from three tables; Items, Missions and linkMissionItem with the following query:

                        var itemList = from x in me.LinkMissionItem
                                   join i in me.items on x.itemID equals i.itemID
                                   join m in me.Missions on x.missionID equals m.MissionID
                                   where m.MissionNo == selectedMission
                                   orderby i.categoryID, i.name
                                   select new { i.itemID, i.name, x.Role, i.Type.typeName, i.Category.categoryName, i.model, i.serialNo, i.Origin.originCountry, i.cost, x.Packaging };



据我了解,查询给出了一个匿名类型从所有三个表中的某些列拉并将其绑定到像这样一个DataGridView:

I understand that the query gives an anonymous type pulling in some columns from all three tables and binding it to a datagridview like so:

dgvMissionItems.DataSource = itemList.ToList();



使得只读DataGridView的。顺便说一句,我目前编程更新包装领域,没有问题。

making the datagridview read only. Incidently, I currently update the Packaging field programatically, no problem.

不过,关于有一个DataGridView的伟大的事情是添加或编辑单个细胞改变它的数据( S)。我需要让用户从linkMissionItem表编辑角色字段。如果我填一个DataGridView只有实体LinkMissionTable(使它modifyable),为用户的信息是不友好和不明确的。

But the great thing about having a datagridview is to add or change data in it by editing the individual cell(s). I need to allow the user to edit the Role field from the linkMissionItem table. If I fill a datagridview with only the entity LinkMissionTable (making it modifyable) the information for the user is unfriendly and unclear.

我误解了,或者我也许在进攻这个问题错误的方式和我在如何完成工作的一些建议迫切需要。我还没有尚未能找到在互联网上或在书中我对这个问题任何事情。

I have misunderstood or am attacking this problem perhaps in the wrong way and am in desperate need of some advice on how to get the job done. I haven't as yet been able to find anything on the Internet or in the books I have on the subject.

推荐答案

您应检索整个实体(与所需的相关实体一起),但禁用DataGridView的的AutoGenerateColumns 功能。那么只有你想显示给用户,而不是通过重新包装他们进入新的对象打破了实体和网格的数据源之间的关系定义列。

You should retrieve the whole entity (along with the related entities required) but disable the AutoGenerateColumns feature of the DataGridView. Then only define the columns you want to show the user instead of breaking the link between the entities and the datasource of the grid by repackaging them into new objects.

这篇关于DataGridView的使用LINQ到实体匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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