更新GridView的数据使用LINQ [英] Updating GridView Data using LINQ

查看:105
本文介绍了更新GridView的数据使用LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要得到更新RowUpdating事件的GridView。我如何使用LINQ更新数据?

我填充使用LINQ也和这里GV是code为:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
         字符串getEntity =的Request.QueryString [ENTITYID];
         INT getIntEntity = Int16.Parse(getEntity);
         使用(OISLinq2SqlVs1DataContext DT =新OISLinq2SqlVs1DataContext())
         {
             VAR TR =从dt.Usersř
                      加入S IN dt.Entities上r.Entity_ID等于s.ID
                      其中,s.ID == getIntEntity
                      新选择
                      {
                          s.Name,
                          r.FirstName,
                          r.LastName,
                          s.Email,
                          //r.Email,
                          r.UserID,
                          r.Pwd,
                          s.Company,
                          s.Description,
                          s.Phone,
                          s.Fax,
                          s.WebSite                      };             gvShowRegistration.DataSource = TR;
             gvShowRegistration.DataBind();
         }    }


解决方案

什么你正在试图做的会用的的LinqDataSource

下面是它一个很好的教程从斯科特谷

<一个href=\"http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx

修改

如果你绝对不能使用LINQ的DS,然后在您的RowUpdating事件你将需要创建一个数据的背景下,重新阅读基础上,DataKey从该行的对象,你重新更新,根据您的事件参数指定新值,然后保存更改。

这将是一个痛苦。三重支票,你可以不通过前所有准备使用LDS。

I have a GridView which needs to get updated on RowUpdating Event. How can I update data using LINQ?

I am populating GV using LINQ too and here is the code for that:

 protected void Page_Load(object sender, EventArgs e)
    {
         string getEntity = Request.QueryString["EntityID"];
         int getIntEntity = Int16.Parse(getEntity);
         using (OISLinq2SqlVs1DataContext dt = new OISLinq2SqlVs1DataContext())
         {
             var tr = from r in dt.Users
                      join s in dt.Entities on r.Entity_ID equals s.ID
                      where s.ID == getIntEntity
                      select new
                      {
                          s.Name,
                          r.FirstName,
                          r.LastName,
                          s.Email,
                          //r.Email,
                          r.UserID,
                          r.Pwd,
                          s.Company,
                          s.Description,
                          s.Phone,
                          s.Fax,
                          s.WebSite

                      };

             gvShowRegistration.DataSource = tr;
             gvShowRegistration.DataBind();
         }

    }

解决方案

What you're trying to do would be best accomplished with a LinqDataSource

Here's a good tutorial on it from Scott Gu

http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx

EDIT

If you absolutely cannot use a Linq DS, then in your RowUpdating event you're going to have to create a new data context, re-read the object based on the DataKey from the row you're updating, assign the new values based on your event args, and then save your changes.

It's going to be a pain. Triple check that you can't use a LDS before going through all that.

这篇关于更新GridView的数据使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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