In asp.net, The GridView is edited, updated, and the total usage example is asp. netgridview.

Source: Internet
Author: User

In asp.net, The GridView is edited, updated, and the total usage example is asp. netgridview.

This example describes how to edit, update, and aggregate a GridView in asp.net. We will share this with you for your reference. The details are as follows:

Front-end code:

<Asp: gridView ID = "tabgv" runat = "server" DataKeyNames = "ysId" ShowFooter = "True" OnRowDataBound = "inline" OnRowCreated = "GridView1_RowCreated" AutoGenerateColumns = "False" Width = "99% "CssClass =" gridview "EnableModelValidation =" True "OnRowCancelingEdit =" inline "OnRowEditing =" inline "OnRowUpdating =" tabgv_RowUpdating "> <RowStyle BorderColor =" Black "BorderStyle =" None "/> <Columns> <asp: boundField DataField = "gsHkrno" HeaderText = "Project No." ReadOnly = "True"> <ItemStyle Width = "5%" HorizontalAlign = "Center"/> <FooterStyle BorderWidth = "0px "/> </asp: boundField> <asp: TemplateField HeaderText = "project name"> <ItemTemplate> <asp: hyperLink ID = "Hyp_gsName" runat = "server" ForeColor = "Blue" Text = '<% # Bind ("gsName") %>'> </asp: hyperLink> </ItemTemplate> <ItemStyle Width = "25%" HorizontalA Lign = "left"/> <FooterTemplate> <asp: Label ID = "Label4" runat = "server" Font-Bold = "True" Text = "merge: "Width =" 61px "> </asp: Label> </FooterTemplate> <FooterStyle BorderColor =" Black "BorderWidth =" 0px "HorizontalAlign =" Right "/> </asp: templateField> <asp: TemplateField HeaderText = "estimated amount"> <ItemTemplate> <asp: label ID = "labgsMoney" runat = "server" Text = '<% # Bind ("gsMoney", "{0: N2}") %>'> </asp: label> </ItemTempl Ate> <ItemStyle Width = "8%" HorizontalAlign = "right"/> <FooterTemplate> <asp: label ID = "labtotalgsMoney" runat = "server" ForeColor = "Red"> </asp: label> </FooterTemplate> <FooterStyle BorderColor = "Black" HorizontalAlign = "Right"/> </asp: TemplateField> <asp: templateField HeaderText = "commencement of work until the end of the year \ n cumulative completion of budget estimates"> <ItemTemplate> <asp: label ID = "labkgzsndwcGsMoney" runat = "server" Text = '<% # Bind ("footMoney", "{0: N2}") %>'> </Asp: Label> <asp: HiddenField ID = "hdnysYear" runat = "server" Value = '<% # Eval ("ysYear ") %> '/> <asp: HiddenField ID = "hdngsId" runat = "server" Value =' <% # Eval ("gsId") %> '/> <asp: hiddenField ID = "hdngsHkrno" runat = "server" Value = '<% # Eval ("gsHkrno") %>'/> <asp: hiddenField ID = "hdnChildFlag" runat = "server" Value = '<% # Eval ("childFlag ") %> '/> </ItemTemplate> <ItemStyle Width = "8%" HorizontalAlign = "rig Ht "/> <FooterTemplate> <asp: Label ID =" labtotalfootMoney "runat =" server "ForeColor =" Red "> </asp: label> </FooterTemplate> <FooterStyle BorderColor = "Black" HorizontalAlign = "Right"/> </asp: TemplateField> <asp: templateField HeaderText = "investment plan amount"> <ItemTemplate> <asp: Label ID = "labtzqysMoney" runat = "server" Text = '<% # Bind ("tzqysMoney ", "{0: N2}") %> '> </asp: Label> </ItemTemplate> <EditItemTemplate> <asp: Tex TBox ID = "txttzqysMoney" runat = "server" Text = '<% # Eval ("tzqysMoney") %> 'style = "text-align: right "Width =" 100px "onKeyPress =" if (event. keyCode! = 46 & event. keyCode! = 45 & (event. keyCode <48 | event. keyCode> 57) event. returnValue = false "> </asp: TextBox> </EditItemTemplate> <ItemStyle Width =" 8% "HorizontalAlign =" right "/> <FooterTemplate> <asp: label ID = "labtotaltzqysMoney" runat = "server" ForeColor = "Red"> </asp: label> </FooterTemplate> <FooterStyle BorderColor = "Black" HorizontalAlign = "Right"/> </asp: TemplateField> <ItemTemplate> <asp: label ID =" LabtzhysMoney "runat =" server "Text = '<% # Bind (" tzhysMoney "," {0: N2} ") %>'> </asp: label> </ItemTemplate> <EditItemTemplate> <asp: TextBox ID = "txttzhysMoney" runat = "server" Text = '<% # Eval ("tzhysMoney ") %> 'style = "text-align: right" Width = "100px" onKeyPress = "if (event. keyCode! = 46 & event. keyCode! = 45 & (event. keyCode <48 | event. keyCode> 57) event. returnValue = false "> </asp: TextBox> </EditItemTemplate> <ItemStyle Width =" 8% "HorizontalAlign =" right "/> <FooterTemplate> <asp: label ID = "labtotaltzhysMoney" runat = "server" ForeColor = "Red"> </asp: label> </FooterTemplate> <FooterStyle BorderColor = "Black" HorizontalAlign = "Right"/> </asp: TemplateField> <asp: TemplateField HeaderText = "Remarks"> <ItemTemplate> <Asp: Label ID = "labtremark" runat = "server" Text = '<% # Eval ("remark") %>'> </asp: label> </ItemTemplate> <EditItemTemplate> <asp: TextBox ID = "txtremark" runat = "server" Text = '<% # Eval ("remark ") %> 'width = "100px"> </asp: TextBox> </EditItemTemplate> <ItemStyle Width = "10%" HorizontalAlign = "Center"/> </asp: templateField> <asp: TemplateField ShowHeader = "False"> <EditItemTemplate> <asp: LinkButton ID = "LkBtnUp Date "runat =" server "CausesValidation =" True "CommandName =" Update "Text =" Update "> </asp: LinkButton> <asp: linkButton ID = "LkBtnCtrl" runat = "server" CausesValidation = "False" CommandName = "Cancel" Text = "Cancel"> </asp: linkButton> </EditItemTemplate> <ItemTemplate> <asp: linkButton ID = "LinkButton2" runat = "server" CausesValidation = "False" CommandName = '<% # Eval ("childFlag "). toString () = "0 "? "": "Edit" %> 'text = '<% # Eval ("childFlag"). ToString () = "0 "? "Lock": "edit" %> '> </asp: LinkButton> </ItemTemplate> <ItemStyle Width = "8%" HorizontalAlign = "Center"/> </asp: templateField> </Columns> </asp: GridView>

Note:

1. Set ReadOnly = "True" to indicate that this column cannot be edited;
2. format the amount by Text = '<% # Bind ("tzhysMoney", "{0: N2}") %>;
3. Key edits, displays, and totals

<asp:TemplateField>  <ItemTemplate>    <asp:Label ID="labtzhysMoney" runat="server" Text='<%# Bind("tzhysMoney", "{0:N2}") %>'></asp:Label>  </ItemTemplate>  <EditItemTemplate>    <asp:TextBox ID="txttzhysMoney" runat="server" Text='<%# Eval("tzhysMoney") %> '      Style="text-align: right" Width="100px"       ></asp:TextBox>  </EditItemTemplate>  <ItemStyle Width="8%" HorizontalAlign="right" />  <FooterTemplate>    <asp:Label ID="labtotaltzhysMoney" runat="server" ForeColor="Red"></asp:Label>  </FooterTemplate>  <FooterStyle BorderColor="Black" HorizontalAlign="Right" /></asp:TemplateField>

Background code:

Actions for row binding:

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. find Control ("Hyp_gsName"); if (hdnChildFlag. Value! = "1") {// set the connection address \ parameter 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;} // calculate the total 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);} // The editing status. Otherwise, an error occurs when you click Edit. 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 // when the status is not editable {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) ;}}}// cancel the edit protected void tabgv_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e) {tabgv. editIndex =-1; string strysYear = (Convert. toInt32 (ysYear)-1 ). toString (); GvDataBind (gsHkrno, "0002", ysYear, "00:00:00", strysYear + "-12-25 00:00:00 ");} // edit protected void tabgv_RowEditing (object sender, GridViewEditEventArgs e) {tabgv. editIndex = e. newEditIndex; string strysYear = (Convert. toInt32 (ysYear)-1 ). toString (); GvDataBind (gsHkrno, "0002", ysYear, "00:00:00", strysYear + "-12-25 00:00:00 ");} // update protected void tabgv_RowUpdating (object sender, Volume 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, "00:00:00", strysYear + "-12-25 00:00:00 ");}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.