您好我想显示每列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>
答案 0 :(得分:2)
试试这个(可能我宁愿使用小数):
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]);
}
}