本文实例讲述了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" + 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程序设计有所帮助。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。