简单的EF绑定 [英] Simple EF binding

查看:77
本文介绍了简单的EF绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



也许这个很容易做,但由于我是EF的新手,我无法弄明白。我有一个DataGridView,我需要绑定到一个包含3个属性,UserID,Name,Type的简单实体。 "类型"字段在数据库中为tinyint,对于不同类型的用户可能包含1,2或3。在DataGridView中,我希望它显示包含Admin,ReadWrite和Readonly而不是1,2和3的Type的下拉列。用户应该只能选择其中一个值(DropDownList样式)并且应该是能够查看和保存更改。任何人都可以帮我解决这个问题吗?

解决方案

你好Syed,

我假设你熟悉GridView控件以及如何使用它的事件处理程序。如果您使用TemplateFields配置了GridView,请将DropDownList控件添加到EditItemTemplate。在GridView的更新事件中(当然GridView处于编辑模式之后),您可以执行以下操作:



Dim ddlPermTypeTemp As DropDownList = GridView1.Rows(GridView1.EditIndex).FindControl( < span style ="color:#a31515; font-size:x-small"> " ddlPermType"
< span style ="font-size:x-small">)

使用 ctx 作为 EFEntities = < span style ="color:#0000ff; font-size:x-small"> EFEntities()



oUser =( 来自 u < span style ="font-size:x-small"> db.Users _
其中 u.UserID = Session(" UserID")_
选择 u).First < span style ="font-size:x-small">

oUser.Permission = ddlPermTypeTemp.SelectedValue
ctx.SaveChanges()

结束使用

以上是从DDL获取值的方法,从数据库中获取User对象,更新Permission值,然后将其保存回DB。我希望这可以帮到你。

(编辑:论坛正在做奇怪的格式化。抱歉。)



Hi,

Maybe this one is pretty easy to do, but since I'm new to EF, I can't figure out how. I have a DataGridView that I need to bind to a simple entity containing 3 properties, UserID, Name, Type. The Type field is tinyint in the DB and may contain 1, 2 or 3 for different types of users. In the DataGridView, I'd like it to show a drop-down column for Type containing Admin, ReadWrite and Readonly instead of 1, 2 and 3. User should be able to select one of these values only (DropDownList style) and should be able to view and save back changes. Can anyone help me with this scenario?

解决方案

Hi Syed,

I'm going to assume that you are familiar with the GridView control and how to use it's Event Handlers.  If you have your GridView configured with TemplateFields, add your DropDownList control to the EditItemTemplate. In the GridView's Updating Event (after the GridView is in Edit mode of course), you can do something like this:

 

Dim ddlPermTypeTemp As DropDownList = GridView1.Rows(GridView1.EditIndex).FindControl("ddlPermType")

Using ctx As EFEntities = New EFEntities()

oUser = (From u In db.Users _
Where u.UserID = Session("UserID") _
Select u).First

oUser.Permission = ddlPermTypeTemp.SelectedValue
ctx.SaveChanges()

End Using

Above is how you would get the value from your DDL, get the User object from your DB, update the Permission value, then save it back to the DB.  I hope this helps you out.

(Edit: Forum is doing weird formatting. Sorry.)


这篇关于简单的EF绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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