vitor_lt_100 2024-12-10 07:34 采纳率: 100%
浏览 7
已结题

.NET WEB C# GridView控件合计行和LinkButton冲突问题

一个GridView,添加了TemplateField里的修改按键。

            <asp:GridView ID="GridView3" runat="server" CssClass="table table-bordered table-hover" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
              <Columns>
              <asp:BoundField DataField="单位" HeaderText="单位" />
              <asp:BoundField DataField="预计金额" HeaderText="预计金额" />
              <asp:BoundField DataField="金额" HeaderText="实际金额" />
              <asp:BoundField DataField="备注" HeaderText="备注" />
              <asp:TemplateField>
                <ItemTemplate>
                  <asp:LinkButton runat="server" CommandName="UpdateById" CommandArgument='<%# Eval("ID") %>' CssClass="btn btn-primary">修改</asp:LinkButton>
                </ItemTemplate>
              </asp:TemplateField>
              </Columns>
            </asp:GridView>

代码里添加了一个合计行。

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("ID", typeof(int));
            dt1.Columns.Add("单位", typeof(string));
            dt1.Columns.Add("预计金额", typeof(decimal));
            dt1.Columns.Add("金额", typeof(decimal));
            dt1.Columns.Add("备注", typeof(string));
            foreach (DataRow row1 in ds1.Tables[0].Rows)
            {
                U_分解合同单位.GetModel(Convert.ToInt32(row1["分解合同单位ID"]));
                U_单位.GetModel(Convert.ToInt32(U_分解合同单位.单位ID));
                dt1.Rows.Add(Convert.ToInt32(row1["ID"]), U_单位.名称, Math.Round(Convert.ToDecimal(row1["预计金额"]), 2), Math.Round(Convert.ToDecimal(row1["金额"]), 2), row1["备注"].ToString());
            }

            dt1.DefaultView.Sort = "ID";

            DataRow totalRow = dt1.NewRow();
            totalRow["ID"] = 99999999;
            totalRow["单位"] = "合计:";
            totalRow["预计金额"] = dt1.Compute("Sum(预计金额)", "");
            totalRow["金额"] = dt1.Compute("Sum(金额)", "");
            totalRow["备注"] = "";
            dt1.Rows.Add(totalRow);

            GridView3.DataSource = dt1;
            GridView3.DataBind();

结果如图,合计行里也有个修改按键,不知道怎么解决。

img

展开全部

  • 写回答

1条回答 默认 最新

  • JavaPub-rodert 博客专家认证 2024-12-11 03:00
    关注

    本答案参考 多次询问 ChatGPT-3.5 后选择的最佳回答,若是满意,还请采纳,谢谢

    要解决GridView控件合计行和LinkButton冲突的问题,可以在GridView的RowDataBound事件中判断当前行是否为合计行,如果是,则不显示修改按钮。以下是具体的代码示例:

    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // 检查行类型是否为合计行(Footer)
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            // 将修改按钮的可见性设置为false
            LinkButton lnkButton = (LinkButton)e.Row.FindControl("LinkButton1"); // 假设你的LinkButton ID为LinkButton1
            if (lnkButton != null)
            {
                lnkButton.Visible = false;
            }
        }
    }
    

    在这段代码中,我们首先检查了当前行是否为合计行(DataControlRowType.Footer)。如果是,我们就找到该行中的LinkButton控件,并将其可见性设置为false,这样合计行就不会显示修改按钮了。

    请确保你的LinkButton控件的ID与代码中的"LinkButton1"相匹配,或者根据实际情况进行调整。同时,你需要在GridView控件中添加OnRowDataBound属性,并将其设置为对应的事件处理方法名,例如:

    <asp:GridView ID="GridView3" runat="server" CssClass="table table-bordered table-hover" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView3_RowDataBound">
        <!-- 列定义 -->
    </asp:GridView>
    

    这样,每当GridView行数据绑定时,都会调用GridView3_RowDataBound方法,从而实现合计行中不显示修改按钮的效果。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 12月18日
  • 已采纳回答 12月11日
  • 创建了问题 12月10日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部