简体   繁体   English

计算启用分页的网格视图中的条目数

[英]count number of entries in grid view with paging enabled

I have a TextBox in which I want to display the number of entries in my GridView . 我有一个TextBox ,我想在其中显示我的GridView的条目数。

count2.Text += (GV.Rows.Count).ToString();

Paging is enabled but it counts only the entries in the first page. 已启用分页,但它仅计算第一页中的条目。

How do I ensure that it counts all entries ? 如何确保计算所有条目?

I tried this : 我试过这个:

         <asp:SqlDataSource ProviderName="System.Data.SqlClient" ID = "sourceProducts" runat = "server" ConnectionString =" <%$ ConnectionStrings:DB %> " SelectCommand = "myqueryhere"  OnSelected="sourceProducts_Selected">




protected void sourceProducts_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        z = e.AffectedRows;
    }

But this gives me some wrong value 但这给了我一些错误的价值

Take a step back. 退后一步。 Where did you get the data from? 你从哪里得到数据? If you are binding to a data table then just count the rows there. 如果绑定到数据表,则只计算那里的行。 If calling to a stored proc with parameters for start and end rows and getting a table back, change it to send a dataset back. 如果使用start和end行的参数调用存储过程并返回表,请将其更改为发回数据集。 The first table in the dataset would be your data, the second a table just containing the count. 数据集中的第一个表是您的数据,第二个表是包含计数的表。

You can use Selected event of SQLDataSource , and fetch the number of records using Affected property like this:- 您可以使用SQLDataSource的 Selected事件,并使用Affected属性获取记录数,如下所示: -

protected void sourceProducts_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    count2.Text = e.AffectedRows.ToString();
}

Edit: 编辑:

protected void Page_Load(object sender, EventArgs e)
{
    System.Data.DataView dataView = (DataView)sourceProducts
                                              .Select(DataSourceSelectArguments.Empty);
    count2.Text = dataView .Count.ToString();
}

Just check this, what will be the result in your selected method 只需检查一下,您selected method的结果是什么

protected void sourceProducts_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
int startRowOnPage = (GridView1.PageIndex * GridView1.PageSize) + 1;
    int lastRowOnPage = startRowOnPage + GridView1.Rows.Count - 1;
    int totalRows = e.AffectedRows;

    count2.Text = "Showing " + startRowOnPage.ToString() +
                  " - " + lastRowOnPage + " of " + totalRows;
}

check this links 检查此链接

Total Row Count SqlDataSource GridView 总行数SqlDataSource GridView

Records count in asp.net gridview with SQL data source & Paging 使用SQL数据源和分页在asp.net gridview中记录计数

or you can add row_number column as rowcount like 或者你可以添加row_number列作为rowcount

select row_number() over(order by pin) a , others column from yourtable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM