Updating GridView Data using LINQ
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.
精彩评论