结合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()事件是注册以下事件
- 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事件
- 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)
- {
- #region Read Excel to Table
- //处理该条记录
- #endregion
- }
- }
- catch (Exception ex)
- {
- #region Loghandle by Tony 2008.11.21
- //string loginid = EmptyString;
- //myLogger.Error(GetErrorMessage(loginid, 1), ex);
- #endregion
- }
- //BindList();
- }
第二种方式,用GVList_RowCommand事件
- 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)
- {
- #region Execute Batch Operations
- if (e.CommandName == "Import" )
- {
- try
- {
- string fullpath = SQLParser.StringParse(e.CommandArgument); //.ToString();
- if (fullpath.Length > 0)
- {
- #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();
- }
- #endregion
- }
第三种方式,在数据不大的情况下,可以用ViewState来缓存DataTable,此时可以直接操作DataTable的Row,只需找到Row的索引即可。
第四种方式,可以用GridViewRow来找到索引,道理同上, 可以参考
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridviewrow.aspx