gpt4 book ai didi

c# - 在 gridview 中显示页脚中每一列的总数

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:59 26 4
gpt4 key购买 nike

你好,我想显示每列 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>

最佳答案

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

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]);
}
}

关于c# - 在 gridview 中显示页脚中每一列的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20165364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com