gridview统计当前页的数据

gridview在每创建一行时都会执行GridView1_RowDataBound(object sender, GridViewRowEventArgs e)方法,我们可以利用这个方法来处理统计,其中的关键是如何获取要统计的数据,可以以这种方式e.Row.Cells[0].Text,但在gridview中如果该列使用的是模板,则不能以此方式获取数据

private decimal orderScore = 0;
private decimal windScore = 0;
private decimal chouShui = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//创建数据行
{
if (e.Row.Cells[7].Text != "")
{
chouShui += Convert.ToDecimal(e.Row.Cells[7].Text);//如果该列使用的是模板,则不能使用
//e.Row.Cells[7].Text来获取要统计的数据
}
if (e.Row.Cells[6].Text != "")
{
windScore += Convert.ToDecimal(e.Row.Cells[6].Text);
}
if (e.Row.Cells[5].Text != "")
{
orderScore += Convert.ToDecimal(e.Row.Cells[5].Text);
}
}
if (e.Row.RowType == DataControlRowType.Footer)//创建gridview的Footer行时
{
e.Row.Cells[0].Text = "总计";
e.Row.Cells[7].Text = chouShui.ToString();
e.Row.Cells[6].Text = windScore.ToString();
e.Row.Cells[5].Text = orderScore.ToString();
}
}

前台代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" PageSize="30" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="GameName" HeaderText="" />
<asp:TemplateField HeaderText="开始时间" SortExpression="startTime">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("startTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ClientIP" HeaderText="" />
<asp:TemplateField HeaderText="" SortExpression="SpareScore">
<ItemTemplate>
<%#string.Format("{0:F}", Eval("SpareScore"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="OrdersScoreCount" DataFormatString="{0:F}" HeaderText="" />
<asp:BoundField DataField="WinScore" DataFormatString="{0:F}" HeaderText="" />
<asp:BoundField DataField="ChouShui" DataFormatString="{0:F}" HeaderText="" >
<ItemStyle Width="70px" />
</asp:BoundField>
</Columns>
</asp:GridView>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值