asp.net, the GridView edit, update, total usage examples _ practical tips

Source: Internet
Author: User

This example describes the GridView edit, update, and aggregate usage in asp.net. Share to everyone for your reference, specific as follows:

Foreground code:

<asp:gridview id= "TABGV" runat= "datakeynames=" Ysid "showfooter=" True "onrowdatabound=" gridview1_
  RowDataBound "onrowcreated=" gridview1_rowcreated "autogeneratecolumns= False" width= "99%" cssclass= "GridView"
  enablemodelvalidation= "True" onrowcancelingedit= "Tabgv_rowcancelingedit" onrowediting= "tabgv_RowEditing" onrowupdating= "tabgv_rowupdating" > <rowstyle bordercolor= "Black" borderstyle= "None"/> <Columns> &l T;asp:boundfield datafield= "Gshkrno" headertext= "project number" readonly= "True" > <itemstyle width= "5%" horizontalalign= "Center"/> <footerstyle borderwidth= "0px"/> </asp:BoundField> <asp:templatefield Headert ext= "project name" > <ItemTemplate> <asp:hyperlink id= "hyp_gsname" runat= "Server" forecolor= "Blue" text= " <%# Bind ("Gsname")%> ' ></asp:HyperLink> </ItemTemplate> <itemstyle width= ' 25% ' Horizon Talalign= "left"/> <FooterTemplate> &lT;asp:label id= "Label4" runat= "Server" font-bold= "True" text= "total:" width= "61px" ></asp:Label> </footerte mplate> <footerstyle bordercolor= "Black" borderwidth= "0px" horizontalalign= "right"/> </asp:template  field> <asp:templatefield headertext= "estimate amount" > <ItemTemplate> <asp:label id= "Labgsmoney" runat= "Server" text= ' <% #Bind ("Gsmoney", "{0:n2}")%> ' ></asp:Label> </ItemTemplate> <i Temstyle width= "8%" horizontalalign= "right"/> <FooterTemplate> <asp:label id= "Labtotalgsmoney" runat= "Server" forecolor= "Red" ></asp:Label> </FooterTemplate> <footerstyle bordercolor= "Blac
      K "horizontalalign=" right "/> </asp:TemplateField> <asp:templatefield headertext=" Start the end of the year \ n cumulative completion estimate "> <ItemTemplate> <asp:label id= "Labkgzsndwcgsmoney" runat= "server" text= ' <%# Bind ("Footmoney", "{0 : N2} ")%> ' ></asp:label> <asp:hiddenfield id= "hdnysyear" runat= "server" value= ' <%# ' Eval ("ysyear")%> '/> & Lt;asp:hiddenfield id= "Hdngsid" runat= "server" Value= ' <%# Eval ("Gsid")%> '/> ' <asp:hiddenfield ID= ' HD Ngshkrno "runat=" server "Value= ' <%# Eval (" Gshkrno ")%> '/> ' <asp:hiddenfield id= ' hdnchildflag ' runat= ' Server "Value= ' <%# Eval (" Childflag ")%> '/> </ItemTemplate> <itemstyle width= ' 8% ' Horizonta lalign= "right"/> <FooterTemplate> <asp:label id= "Labtotalfootmoney" runat= "Server" forecolor= "R Ed "></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:textbox id= "Txttzqysmoney" runat= "server" text= ' <%# Eval ("Tzqysmoney") %> ' style= ' text-align:right "width=" 100px "onkeypress=" if (event.keycode!=46 && Event.key Code!=45 && (event.keycode<48 | | event.keycode>57)) event.returnvalue=false "></asp:TextBox> &
        lt;/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> <asp:templa tefield> <ItemTemplate> <asp:label id= "Labtzhysmoney" runat= "server" text= ' <%# Bind ("Tzhysmon EY "," {0:n2} ")%> ' ></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:tex Tbox id= "Txttzhysmoney" Runat= "Server" text= ' <%# Eval ("Tzhysmoney")%> ' style= ' text-align:right ' width= ' 100px ' ONKEYPR Ess= "If" (event.keycode!=46 && event.keycode!=45 && (event.keycode<48 | | event.keycode>57)) Event.returnvalue=false "></asp:TextBox> </EditItemTemplate> <itemstyle width=" 8% "Horizontal align= "right"/> <FooterTemplate> <asp:label id= "Labtotaltzhysmoney" runat= "Server" forecolor= "R Ed "></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")%&gt ; ' Width= ' 100px ' ></asp:textbox> </EditItemTemplate> <itemstyle width= "10%" horizontalalign= "Center"/> </asp:templ atefield> <asp:templatefield showheader= "False" > <EditItemTemplate> <asp:linkbutton ID
        = "Lkbtnupdate" runat= "Server" causesvalidation= "True" commandname= "Update" text= "Updates" ></asp:LinkButton> <asp:linkbutton id= "Lkbtnctrl" runat= "Server" causesvalidation= "False" commandname= "Cancel" text= "Cancel" ></asp:LinkButton> </EditItemTemplate> <ItemTemplate> <asp:linkbutton id= "Link Button2 "runat=" Server "causesvalidation=" False "Commandname= ' <% #Eval (" Childflag "). ToString () = = "0"? "": "Edit"%> ' text= ' <% #Eval ("Childflag"). ToString () = = "0"? "Lock": "Edit"%> ' ></asp:LinkButton> </ItemTemplate> <itemstyle width= "8%" horizontalalign= "Cen

 ter "/> </asp:TemplateField> </Columns> </asp:GridView>

Attention:

1, by setting readonly= "True" means that this column can not be edited;
2, through the text= ' <%# Bind ("Tzhysmoney", "{0:n2}")%> ' for the amount of format;
3, the focus of the editing \ Display \ Total

<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:

Action at line 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.findcontrol ("Hyp_gsname"); if (hdnchildflag.value!= "1") {//Set 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 total Label Labgsmoney = (label) (E.row.findcontrol ("Labgsmoney")); if (Labgsmoney!= null | | | labgsmoney.text!= "")
      {Gsmoneytotal + = Convert.todecimal (Labgsmoney.text);
      The label Labkgzsndwcgsmoney = (label) (E.row.findcontrol ("Labkgzsndwcgsmoney")); if (Labkgzsndwcgsmoney!= null | | labkgzsndwcgsmoney.text!= "") {footmoneytotal = Convert.todecimal (labkg
      Zsndwcgsmoney.text);
      }//Edit status, or click Edit Error, can not find the control if ((E.row.rowstate & datacontrolrowstate.edit) = = Datacontrolrowstate.edit)
        {TextBox Txttzqysmoney = (textbox) (E.row.findcontrol ("Txttzqysmoney")); if (Txttzqysmoney!= null | | txttzqysmoney.text!= "") {tzqysmoneytotal = Convert.todecimal (txttzqysmo Ney.
        Text);
        TextBox Txttzhysmoney = (textbox) (E.row.findcontrol ("Txttzhysmoney")); if (Txttzhysmoney!= null | | txttzhysmoney.text!= "") {tzhysmoneytotal = Convert.todecimal (txttzhysmo Ney.
        Text); {Label Labtzqysmoney = (label) (E.row.findcontrol ("Labt")} else//non-edit stateZqysmoney ")); if (Labtzqysmoney!= null | | labtzqysmoney.text!= "") {tzqysmoneytotal = Convert.todecimal (labtzqysmo Ney.
        Text);
        The label Labtzhysmoney = (label) (E.row.findcontrol ("Labtzhysmoney")); if (Labtzhysmoney!= null | | labtzhysmoney.text!= "") {tzhysmoneytotal = Convert.todecimal (labtzhysmo Ney.
        Text); }}//Cancel edit protected void Tabgv_rowcancelingedit (object sender, Gridviewcancelediteventargs e) {TABGV.E
    Ditindex =-1; String strysyear = (Convert.ToInt32 (ysyear)-1).
    ToString ();
Gvdatabind (Gshkrno, "0002", Ysyear, "1900-01-01 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, "1900-01-01 00:00:00", Strysyear + "-12-25 00:00:00"); }//update protected void Tabgv_rOwupdating (object sender, Gridviewupdateeventargs e) {string sqlstr = "Update ys_ysitem_list set tzqysmoney= @tzqysMon
    ey,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");

 }

More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."

I hope this article will help you to ASP.net program design.

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.