
GridView操作(可以是删除、导入、更新等)单条记录的N种方式
结合GridView自身的特点,总结出操作(可以是删除、导入、更新等)单条记录的N种方式
首先,前台文件内容如下:
<asp:GridView ID="GVList" runat="server" ShowFooter="true" AutoGenerateColumns="False"
BorderStyle="Solid" BorderColor="#ffffff" GridLines="Horizontal" CellSpacing="1"
Width="100%" HorizontalAlign="NotSet" BorderWidth="0px" EnableViewState="true"
DataKeyNames="PKID">
<Columns>
<asp:TemplateField>
<HeaderStyle Width="60px" BackColor="#1C5E55" ForeColor="White" />
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="PKID" Text='<%# DataBinder.Eval(Container.DataItem,"PKID")%>' runat="server"
Visible="true" Width="10"/>
<asp:Label ID="FilePath" Text='<%# DataBinder.Eval(Container.DataItem,"FilePath")%>' runat="server"
Visible="true" Width="700"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle Width="60px" />
<HeaderTemplate>
操作</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="cmdImport" ForeColor="Red" Text="操作" CssClass="ElementNavigation"
CausesValidation="false" runat="server" CommandName="Import" OnClientClick="javascript:return confirm('确定操作已选择的数据吗?')" />
</ItemTemplate>
</asp:TemplateField><asp:CommandField DeleteText="操作" ShowDeleteButton="true" ButtonType="Button" HeaderStyle-Width="40px" />

</Columns>
</asp:GridView>
其次:在后台Page_Load()事件是注册以下事件

Code
if (GVList != null)

{
GVList.RowDataBound += new GridViewRowEventHandler(GVList_RowDataBound);
GVList.RowCommand += new GridViewCommandEventHandler(GVList_RowCommand);
GVList.RowDeleting += new GridViewDeleteEventHandler(GVList_RowDeleting);
}
同时添加以下事件
private void GVList_RowUpdating(object sender, GridViewUpdateEventArgs e)

{ }
现分别说明各事件的作用如下:
第一种操作方式,用GVList_RowDeleting事件

Code
protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e)

{
if (e.Row.RowType == DataControlRowType.DataRow)

{
LinkButton lbUpdate = (LinkButton)e.Row.FindControl("cmdImport");

if (lbUpdate != null)
{ lbUpdate.CommandArgument = SQLParser.StringParse(DataBinder.Eval(e.Row.DataItem, "FilePath")); }
}
}
private void GVList_RowDeleting(object sender, GridViewDeleteEventArgs e)

{GridView a = (GridView)sender;
try

{
string fullpath = SQLParser.StringParse(a.DataKeys[e.RowIndex]["FilePath"]);//.ToString();
if (fullpath.Length > 0)

{

Read Excel to Table#region Read Excel to Table
//处理该条记录
#endregion
}

}
catch (Exception ex)

{

Loghandle by Tony 2008.11.21#region Loghandle by Tony 2008.11.21
//string loginid = EmptyString;
//myLogger.Error(GetErrorMessage(loginid, 1), ex);
#endregion
}
//BindList();
}
第二种方式,用GVList_RowCommand事件

Code
protected void GVList_RowDataBound(object sender, GridViewRowEventArgs e)

{
if (e.Row.RowType == DataControlRowType.DataRow)

{
LinkButton lbUpdate = (LinkButton)e.Row.FindControl("cmdImport");

if (lbUpdate != null)
{ lbUpdate.CommandArgument = SQLParser.StringParse(DataBinder.Eval(e.Row.DataItem, "FilePath")); }
}
}
private void GVList_RowCommand(object sender, GridViewCommandEventArgs e)

{

Execute Batch Operations#region Execute Batch Operations
if (e.CommandName == "Import")

{
try

{
string fullpath = SQLParser.StringParse(e.CommandArgument);//.ToString();
if (fullpath.Length > 0)

{

Read Excel to Table#region Read Excel to Table
//处理该条记录
#endregion

}
}
catch (Exception ex)

{

Loghandle by Tony 2008.11.21Loghandle by Tony 2008.11.21#region Loghandle by Tony 2008.11.21
//string loginid = EmptyString;
//myLogger.Error(GetErrorMessage(loginid, 1), ex);
#endregion
}
//BindList();
}
#endregion
}
第三种方式,在数据不大的情况下,可以用ViewState来缓存DataTable,此时可以直接操作DataTable的Row,只需找到Row的索引即可。
第四种方式,可以用GridViewRow来找到索引,道理同上, 可以参考
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridviewrow.aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)