使用LinqToSql进行Gridview更新 [英] Gridview Update using LinqToSql

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

问题描述

使用LinqToSql更新和删除Gridview。

Gridview Updating and Deleting Using LinqToSql.

推荐答案

实现GridView编辑,更新和删除:

让我们开始实现编辑和更新功能。首先,您需要添加ComandField按钮,它们代表编辑和更新链接按钮。

您可以随时使用设计器添加编辑,更新和取消按钮。

< pre lang =c#>& lt; asp:commandfield ShowEditButton =True& gt;

& lt; / asp:commandfield& ; gt;

& lt; asp:commandfield ShowDeleteButton =True& gt;

& lt; / asp:commandfield& gt;

下一个任务是让GridView进入编辑模式。这非常简单,可以通过使用GridView_RowEditing事件来实现。

protected void gvProducts_RowEditing(object sender,GridViewEditEventArgs e)

{

gvProducts.EditIndex = e.NewEditIndex;

PopulateProducts();

}

接下来,让我们看一下Update方法。 Update的代码在GridView控件的GridView_RowUpdating事件中实现。

protected void gvProducts_RowUpdating(object sender,GridViewUpdateEventArgs e)

{

//编写编辑代码

}

首先,我创建了一个NorthwindRepository的新实例。然后我得到Product的主键,它被设置为GridView控件中的DataKeyNames属性。我从文本框中获取了编辑过的文本。我使用productID从存储库中获取Product实例。最后我调用了SaveProduct方法。您也可以调用SubmitChanges但在SaveProduct中我将从应用程序缓存中删除Products。

public void SaveProduct()

{

//从缓存中删除产品







CacheRepository.Remove(Products);

base.SubmitChanges();

}

< / pre>





在上图中您可以看到产品名称已更新。
Implementing GridView Edit, Update and Delete:
Let’s start with implementing the edit and the update feature. First you need to add the ComandField buttons with will represent the edit and update link buttons.
You can always use designer to add the edit, update and cancel buttons.
<pre lang="c#">&lt;asp:commandfield ShowEditButton="True"&gt;
&lt;/asp:commandfield&gt;
&lt;asp:commandfield ShowDeleteButton="True"&gt;
&lt;/asp:commandfield&gt;
Next task is to get the GridView into edit mode. This is pretty simple and can be achieved by using the GridView_RowEditing event.
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
{
gvProducts.EditIndex = e.NewEditIndex;
PopulateProducts();
}
Next, let’s check out the Update method. The code for the Update is implemented inside the GridView_RowUpdating event of the GridView control.
protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//write code for Editing
}
First I created a new instance of the NorthwindRepository. Then I get the primary key of the Product which is set as the DataKeyNames property in the GridView control. I get the edited text from the textbox. I get the Product instance from the repository using the productID. And then finally I called the SaveProduct method. You can also call SubmitChanges but in SaveProduct I am removing the Products from the application cache.
public void SaveProduct()
{
// remove the products from the cache



CacheRepository.Remove("Products");
base.SubmitChanges();
}
</pre>


In the image above you can see that the product name has been updated.


试试这个 [ ^ ]


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

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