设置标签文本的GridView的总行数 [英] set label text to total row count of gridview
问题描述
我使用的SQL数据库作为我的.aspx页面上的SqlDataSourceControl数据源的存储过程。然后我使用SqlDataSourceControl作为我的网页上的GridView的数据源。分页设置为true的GridView控件。我想要做的是设置一个标签的文本,以在GridView行的总数。我可以用这code
I'm using a stored procedure in a sql database as the data source for a SqlDataSourceControl on my .aspx page. I'm then using the SqlDataSourceControl as the data source for a gridview on my page. Paging is set to true on the gridview. What i would like to do is set the text of a label to the total number of rows in the gridview. I can use this code
'labelRowCount.Text = GridView2.Rows.Count & " layers found"
返回每页结果数,但它并没有给我总数。我已经看了好几个地方,并找到解决办法都没有成功。
to return the number of results per page, but it doesn't give me the total. I've looked in several places and haven't been successful in finding a solution.
推荐答案
您应该使用的GridView控件绑定到(grid.DataSource)的底层数据源。例如,如果你已经绑定网格到数据表,然后只投网格数据源插入DataTable和使用rows.count属性来获取总记录数。另一种办法是将其设置为前栅格,让你可以记录直接计算得到的网格数据源对象的引用。
You should use the underlying datasource that the gridview is bound to (grid.DataSource). For instance if you have bound the grid to a datatable then just cast the grids datasource into the datatable and the use the rows.count property to get the total record count. Another alternative would be to get a reference to the the grids datasource object before you set it to the grid so you can get the record count directly.
因此,例如,(假设你被绑定到数据表)
So for example (assuming you are bound to a DataTable)
int count = ((DataTable)grid.DataSource).Rows.Count;
享受!
这篇关于设置标签文本的GridView的总行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!