【问题标题】:Show total of every column in footer within a gridview在gridview的页脚中显示每一列的总数
【发布时间】:2013-12-08 13:02:44
【问题描述】:

您好,我想显示每列 7 到 18 的每列总数。使用此代码,它可以为我提供所有列的总数,并在页脚的每个单元格中显示它。我想在等效的页脚单元格中显示每一列的总数。我在 asp 中使用 asp:BoundFields。

示例:column1 sum =2,column2 sum =3,column3 sum=4,使用此代码为页脚的所有单元格提供相同的总数 2+3+4=9。我想在页脚中显示.cell[1] =2,footer.cell[2] =3,footer.cell[4] =4(以编程方式)。每列的总数,而不是所有列的总数。
有什么建议吗?

private int TotalStats = (int)0;
 protected void GridViewHomeTeamStats_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 7; i <= 18; i++){
        // check row type
        if (e.Row.RowType == DataControlRowType.DataRow)
            // if row type is DataRow, add ...                
        {
            TotalStats += Convert.ToInt32(e.Row.Cells[i].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
            // If row type is footer, show calculated total value
            e.Row.Cells[i].Text = String.Format("{0:0.##}", TotalStats);
            }
    }

谢谢。你能帮我处理这三列吗?在这些列中,值类似于:3/4 ,5/6 等。我想在等效的页脚单元格中显示这些列的总和和除数。例如,如果一列有 2 行,值为 1/2 , 2/4 我想在页脚单元格中显示 1+2/2+4=0.5=50% 。 这是来自同一个 GridViewHomeTeamStats 的 asp 代码:

 <asp:GridView ID="GridViewHomeTeamStats" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceHomeTeam" ShowFooter="True" OnRowDataBound="GridViewHomeTeamStats_RowDataBound">
    <Columns>            
        <asp:TemplateField HeaderText="FT" SortExpression="FREE_THROW_MADE">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>'></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="2P" SortExpression="2POINTS_MADE">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("2POINTS_MADE") + "/" + Eval("2POINTS_ATTEMPT") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="3P" SortExpression="3POINT_MADE">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("3POINT_MADE") + "/" + Eval("3POINT_ATTEMPT") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

【问题讨论】:

  • 不行,报错?问题是什么?
  • 我编辑了这个问题以便更加理解

标签: c# asp.net gridview footer


【解决方案1】:

试试这个(可能我宁愿使用十进制):

int[] TotalStats = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

protected void GridViewHomeTeamStats_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int i = 7; i <= 18; i++)
    {
        // check row type
        if (e.Row.RowType == DataControlRowType.DataRow)
        // if row type is DataRow, add ...                
        {
            TotalStats[i-7] += Convert.ToInt32(e.Row.Cells[i].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
            // If row type is footer, show calculated total value
            e.Row.Cells[i].Text = String.Format("{0:0.##}", TotalStats[i-7]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多