计算gridview / DataSource中的总行数

时间:2017-11-21 18:43:20

标签: sql asp.net vb.net

我有一个Gridview,其中充满了池竞赛的注册成员。它目前有2页,在底部我试图显示这个例子:“256个玩家中有23个已注册”作为标签。但是,在这个例子中,它只计算了第1页的20个注册玩家,然后如果你切换到第2页,它说“256个玩家中有3个已注册”。如何更改此项以计算所有页面中已注册玩家的总数(目前应为23)。这就是我的......

lblPlayersCount.Text = gdvEntrants.Rows.Count.ToString & " out of " & session("maxPlayers") & " players have registered."

SQL DataSource就像这样......

<asp:SqlDataSource ID="DSFixtures" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="
SELECT 
tblFixtures.player1, tblFixtures.resultPlayer1,
(SELECT contactname FROM tblaccounts WHERE (accountID = 
tblFixtures.player1)) AS player1_name,
tblFixtures.player2, tblFixtures.resultPlayer2,
(SELECT contactname FROM tblaccounts WHERE (accountID = tblFixtures.player2)) AS player2_name,
tblFixtures.compID, 
tblFixtures.round
FROM tblFixtures INNER JOIN tblCompetitions ON tblFixtures.compID = 
tblCompetitions.compID WHERE tblFixtures.compID = @Event_ID AND round = @Round ">


<SelectParameters>
    <asp:QueryStringParameter QueryStringField="compID" Name="Event_ID" />
    <asp:QueryStringParameter QueryStringField="round" Name="Round" />
</SelectParameters>

</asp:SqlDataSource>

然后gridview开始..

            <asp:Gridview ID="gdvFixtures" visible="true" width="100%"  runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="compID" DataSourceID="DSFixtures" PageSize="20" AllowSorting="True">
Bla Bla Bla...

1 个答案:

答案 0 :(得分:0)

GridViewRowCollection.Count仅在使用分页时计算当前GridView页面中的所有行。要计算GridView显示的整行,您需要从数据源级别开始计算,即SqlDataSource

您可以在后面的代码中处理SqlDataSource的{​​{3}}事件,并使用Selected整数属性来显示所有输出行的计数:

Protected Sub DSFixtures_Selected(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
    lblPlayersCount.Text = e.AffectedRows & " out of " & Session("maxPlayers") & " players have registered."
End Sub

类似问题:

AffectedRows