在GridView的添加摘要行一列 [英] Add summary row in Gridview for one column

查看:125
本文介绍了在GridView的添加摘要行一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 GridView控件,我想在premiseScore得分(第三列)的页脚总和补充。

我怎样才能做到这一点?


                

 < ASP:BoundField的数据字段=premiseUno的HeaderText ='<%$资源:语言,GRD premiseUno%GT;'只读=真SORTEX pression =premiseUno>
                        < HeaderStyle的CssClass =gHeaderStyle/>
                        < ItemStyle的CssClass =gControlStyle/>
                        < / ASP:BoundField的>                         < ASP:BoundField的数据字段=premiseName的HeaderText ='<%$资源:语言,GRD premisesName%GT;'
                            只读=真SORTEX pression =GRD premisesName>
                            < HeaderStyle的CssClass =gHeaderStyle/>
                        < ItemStyle的CssClass =gControlStyle/>
                        < / ASP:BoundField的>                        < ASP:BoundField的数据字段=premiseScore的HeaderText ='<%$资源:语言,GRD premiseScore%GT;'只读=真SORTEX pression =premiseScore>
                        < HeaderStyle的CssClass =gHeaderStyle/>
                        < ItemStyle的CssClass =gControlStyle/>
                        < / ASP:BoundField的>                < ASP:的TemplateField ShowHeader =FALSE的HeaderText ='<%$资源:语言,btnDelete%GT;'>
                    <&ItemTemplate中GT;
                        < ASP:LinkBut​​ton的ID =LinkBut​​ton1=服务器的CausesValidation =FALSE
                            的CommandName =删除文本=删除CommandArgument ='<%#的eval(premiseUno)%>'的onclick =LinkBut​​ton1_Click>< / ASP:LinkBut​​ton的>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>            < /专栏>


解决方案

如果您转换绑定字段到模板字段,可以进入控制控股premise得分值:

 < ASP:的TemplateField的HeaderText =测试>
     <&ItemTemplate中GT;
         < ASP:标签=服务器ID =testLabel文本='<%#的eval(premiseScore)%>'>< / ASP:标签>
     < / ItemTemplate中>
< / ASP:的TemplateField>

您可以然后执行以下操作来计算在GridView的数据绑定事件的总和:

 保护无效Grid_DataBound(对象发件人,EventArgs的发送)
{
    GridViewRow footerRow = grid.FooterRow;
    VAR和=(从grid.Rows GridViewRow行选择((标签)row.FindControl(testLabel))文本。).SUM(D => Convert.ToInt16(D));    footerRow.Cells [0]。文本= sum.ToString();
}

我这里假设所有的值都是整数,但很容易汇入作业转换为其他值类型。

I have this GridView, and I want to add in the footer summation of PremiseScore score (third column).

How I can do this?

            <asp:BoundField DataField="PremiseUno" HeaderText='<%$ Resources:Language, grdPremiseUno %>' ReadOnly="True" SortExpression="PremiseUno"  >
                        <HeaderStyle CssClass="gHeaderStyle" />
                        <ItemStyle CssClass="gControlStyle" />
                        </asp:BoundField>

                         <asp:BoundField DataField="PremiseName" HeaderText='<%$ Resources:Language, grdPremisesName %>'
                            ReadOnly="True" SortExpression="grdPremisesName" >
                            <HeaderStyle CssClass="gHeaderStyle" />
                        <ItemStyle CssClass="gControlStyle" />
                        </asp:BoundField>

                        <asp:BoundField DataField="PremiseScore" HeaderText='<%$ Resources:Language, grdPremiseScore %>' ReadOnly="True" SortExpression="PremiseScore"  >
                        <HeaderStyle CssClass="gHeaderStyle" />
                        <ItemStyle CssClass="gControlStyle" />
                        </asp:BoundField>

                <asp:TemplateField ShowHeader="False" HeaderText= '<%$ Resources:Language, btnDelete %>'>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                            CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("PremiseUno") %>'  onclick="LinkButton1_Click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>



            </Columns>

解决方案

If you convert the bound field to a template field, you can access the control holding the value of premise score:

<asp:TemplateField HeaderText="test">
     <ItemTemplate>
         <asp:Label runat="server" ID="testLabel" Text='<%# Eval("PremiseScore") %>'></asp:Label>
     </ItemTemplate>               
</asp:TemplateField>

You can then do the following to compute the sum in the Databound-event of the gridview:

protected void Grid_DataBound(Object sender, EventArgs e)
{
    GridViewRow footerRow = grid.FooterRow;
    var sum = (from GridViewRow row in grid.Rows select ((Label)row.FindControl("testLabel")).Text).Sum(d => Convert.ToInt16(d));

    footerRow.Cells[0].Text = sum.ToString();
}

I assume here that all values are ints, but it´s easily convertible to other value types.

这篇关于在GridView的添加摘要行一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆