欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-27 09:36:46
本文实例讲述了asp.net中gridview编辑,更新,合计用法。分享给大家供大家参考,具体如下: 前台代码:

本文实例讲述了asp.net中gridview编辑,更新,合计用法。分享给大家供大家参考,具体如下:

前台代码:

<asp:gridview id="tabgv" runat="server" 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>
    <asp:boundfield datafield="gshkrno" headertext="项目编号" readonly="true">
      <itemstyle width="5%" horizontalalign="center" />
      <footerstyle borderwidth="0px" />
    </asp:boundfield>
    <asp:templatefield headertext="项目名称">
      <itemtemplate>
        <asp:hyperlink id="hyp_gsname" runat="server" forecolor="blue" text='<%# bind("gsname") %>'></asp:hyperlink>
      </itemtemplate>
      <itemstyle width="25%" horizontalalign="left" />
      <footertemplate>
        <asp:label id="label4" runat="server" font-bold="true" text="合 计:" width="61px"></asp:label>
      </footertemplate>
      <footerstyle bordercolor="black" borderwidth="0px" horizontalalign="right" />
    </asp:templatefield>
    <asp:templatefield headertext="概算金额">
      <itemtemplate>
        <asp:label id="labgsmoney" runat="server" text='<%#bind("gsmoney", "{0:n2}") %>'></asp:label>
      </itemtemplate>
      <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="开工至上年底\n累计完成概算">
      <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="right" />
      <footertemplate>
        <asp:label id="labtotalfootmoney" runat="server" forecolor="red"></asp:label>
      </footertemplate>
      <footerstyle bordercolor="black" horizontalalign="right" />
    </asp:templatefield>
    <asp:templatefield headertext="投资计划金额">
      <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.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>
    <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="备注">
      <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="lkbtnupdate" runat="server" causesvalidation="true" commandname="update"
          text="更新"></asp:linkbutton>
        <asp:linkbutton id="lkbtnctrl" runat="server" causesvalidation="false" commandname="cancel"
          text="取消"></asp:linkbutton>
      </edititemtemplate>
      <itemtemplate>
        <asp:linkbutton id="linkbutton2" runat="server" causesvalidation="false" commandname='<%#eval("childflag").tostring()== "0" ? "":"edit"%>'
          text='<%#eval("childflag").tostring()== "0" ? "锁定":"编辑"%>'></asp:linkbutton>
      </itemtemplate>
      <itemstyle width="8%" horizontalalign="center" />
    </asp:templatefield>
  </columns>
</asp:gridview>

注意:

1、通过设置 readonly="true" 表示此列不可以编辑;
2、通过text='<%# bind("tzhysmoney", "{0:n2}") %>'进行金额格式化;
3、重点的编辑\显示\合计

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

后台代码:

行绑定时的操作:

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缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。