asp.net中GridView编辑,更新,合计用法示例(2)

<asp:TemplateField> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("tzhysMoney", "{0:N2}") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" Text='<%# Eval("tzhysMoney") %> ' ></asp:TextBox> </EditItemTemplate> <ItemStyle HorizontalAlign="right" /> <FooterTemplate> <asp:Label runat="server" ForeColor="Red"></asp:Label> </FooterTemplate> <FooterStyle BorderColor="Black" HorizontalAlign="Right" /> </asp:TemplateField>

后台代码:

行绑定时的操作:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow gr = e.Row; if (gr.RowType == DataControlRowType.DataRow) { HiddenField hdnysYear = (HiddenField)e.Row.FindControl("hdnysYear"); HiddenField hdngsHkrno = (HiddenField)e.Row.FindControl("hdngsHkrno"); HiddenField hdnChildFlag = (HiddenField)e.Row.FindControl("hdnChildFlag"); HyperLink hy = (HyperLink)e.Row.FindControl("Hyp_gsName"); if (hdnChildFlag.Value!="1") { //设置连接地址\参数 hy.NavigateUrl = "ys_edit_right.aspx?gsHkrno=" + hdngsHkrno.Value + "&sysObjNo=" + sysObjNo + "&ysYear=" + hdnysYear.Value; hy.Font.Underline = true; hy.ForeColor = System.Drawing.Color.Blue; hy.Target = "mainFrame"; } else { hy.ForeColor = System.Drawing.Color.Black; } //计算合计 Label labgsMoney = (Label)(e.Row.FindControl("labgsMoney")); if (labgsMoney != null || labgsMoney.Text != "") { gsMoneytotal += Convert.ToDecimal(labgsMoney.Text); } Label labkgzsndwcGsMoney = (Label)(e.Row.FindControl("labkgzsndwcGsMoney")); if (labkgzsndwcGsMoney != null || labkgzsndwcGsMoney.Text != "") { footMoneytotal += Convert.ToDecimal(labkgzsndwcGsMoney.Text); } //编辑状态时,不然点击编辑时会出错,找不到控件 if ((e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit) { TextBox txttzqysMoney = (TextBox)(e.Row.FindControl("txttzqysMoney")); if (txttzqysMoney != null || txttzqysMoney.Text != "") { tzqysMoneytotal += Convert.ToDecimal(txttzqysMoney.Text); } TextBox txttzhysMoney = (TextBox)(e.Row.FindControl("txttzhysMoney")); if (txttzhysMoney != null || txttzhysMoney.Text != "") { tzhysMoneytotal += Convert.ToDecimal(txttzhysMoney.Text); } } else //非编辑状态时 { Label labtzqysMoney = (Label)(e.Row.FindControl("labtzqysMoney")); if (labtzqysMoney != null || labtzqysMoney.Text != "") { tzqysMoneytotal += Convert.ToDecimal(labtzqysMoney.Text); } Label labtzhysMoney = (Label)(e.Row.FindControl("labtzhysMoney")); if (labtzhysMoney != null || labtzhysMoney.Text != "") { tzhysMoneytotal += Convert.ToDecimal(labtzhysMoney.Text); } } } } //取消编辑 protected void tabgv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { tabgv.EditIndex = -1; string strysYear = (Convert.ToInt32(ysYear) - 1).ToString(); GvDataBind(gsHkrno, "0002", ysYear, "1900-01-01 00:00:00", strysYear + "-12-25 00:00:00"); } //编辑 protected void tabgv_RowEditing(object sender, GridViewEditEventArgs e) { tabgv.EditIndex = e.NewEditIndex; string strysYear = (Convert.ToInt32(ysYear) - 1).ToString(); GvDataBind(gsHkrno, "0002", ysYear, "1900-01-01 00:00:00", strysYear + "-12-25 00:00:00"); } //更新 protected void tabgv_RowUpdating(object sender, GridViewUpdateEventArgs e) { string sqlstr = "update ys_ysitem_list set tzqysMoney=@tzqysMoney,tzhysMoney=@tzhysMoney,remark=@remark where ysId=@ysId"; SqlParameter[] para ={new SqlParameter("@tzqysMoney",SqlDbType.VarChar), new SqlParameter("@tzhysMoney",SqlDbType.VarChar), new SqlParameter("@remark",SqlDbType.VarChar), new SqlParameter("@ysId",SqlDbType.VarChar) }; para[0].Value = ((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txttzqysMoney"))).Text.ToString().Trim(); para[1].Value = ((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txttzhysMoney"))).Text.ToString().Trim(); para[2].Value = ((TextBox)(tabgv.Rows[e.RowIndex].FindControl("txtremark"))).Text.ToString().Trim(); para[3].Value = tabgv.DataKeys[e.RowIndex].Value.ToString(); pms.SqlHelper.ExecuteNonQuery(CommandType.Text, sqlstr, para); tabgv.EditIndex = -1; string strysYear = (Convert.ToInt32(ysYear) - 1).ToString(); GvDataBind(gsHkrno, "0002", ysYear, "1900-01-01 00:00:00", strysYear + "-12-25 00:00:00"); }

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjysyw.html