Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I need to get the total number of items/records returned into GridView.
If I just do myGridView.Rows.Count, then it just returns me the total number
of items on the page.
But if I have, 10 pages (10 records per page) and 98 records total,
Rows.Count will return me 10 on the first 9 pages, and 8, on the last page.
But, I need to somehow get access to total record count which is 98. What
is the proper/easy way to accomplish that?
Posted 24-Feb-11 20:32pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It'd be helpful if you mentioned if you're using an SQLDataSource or an ObjectDataSource for your grid view
 
If ObjectDataSource,
Handle the selected event. The ObjectDataSourceStatusEventArgs 's member 'ReturnType' will have your data source from which you can get the count
Eg:
int RecordsCount = ((DataSet)e.ReturnValue).Tables[0].Rows.Count;
Or the ReturnValue could even be a DataView. It's better you check.
 
If using an SqlDataSource,
Handle the selected Event
The SqlDataSourceStatusEventArgs's member 'AffectedRows' will have the count
Eg:
SqlDataSource1_Selected(Object sender, 
System.Web.UI.WebControls.SqlDataSourceStatusEventArgs e)
{
   e.AffectedRows 
...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi
You need to Set Total Records when you assign DataSource to the Grid.
 
e.g.
int TotalRecord = dt.Rows.Count(); //This is total number of records in gridview
GridView1.DataSource = dt;
GridView1.DataBind();
 

Thanks,
Imdadhusen
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
The easy way to get the Total count is.
 
1. First get the gridView DataSource. e.g
 
you have bind a DataSet then get the DataSet form the GridView.

like
DataSet DS = (DataSet) MygridView.DataSource;
 

2.Then count the no. of row in it.
 
like
int count = DS.Tables[0].Rows.Count;
 
I hope this will solve your problem.
 
Below is method that will return the Total number of Row.
 
private int GetTotalRow()
{
int Rowcount = 0;
try
{
DataSet DS = (DataSet) MygridView.DataSource;
Rowcount = DS.Tables[0].Rows.Count;
}
catch
{
throw;
}
return Rowcount ;
}
 
Thanks
  Permalink  
v2
Comments
rajivpande86 at 25-Feb-11 14:34pm
   
That should work.Just before gridview comes into play, count the rows and then the gridview think we still unable to count the total no.of rows.
anuj barthwal at 19-Jul-13 23:18pm
   
Gridview1.Rows.Count will return the number of rows in Gridview
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Gridview1.Rows.Count will return the number of rows in Gridview
  Permalink  
Comments
CHill60 at 20-Jul-13 14:31pm
   
The OP has already tried that ... "If I just do myGridView.Rows.Count, then it just returns me the total number of items on the page.". And his question was answered two years ago.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Here is a sample i'm providing, i've a label in a templated field at the footer.

 

protected void Page_Load(object sender, EventArgs e)
{
 
GridView1.DataSource = EmployeeByCountry("UK");
GridView1.DataBind();
Label lblFooter = GridView1.FooterRow.FindControl("lblFooter") as Label;
lblFooter.Text = GetNoOfRows().ToString();
}
 
You can define the function "GetNoOfRows()" using scalarquery i,e; by using COUNT() function of SQL, to get no of rows of the table.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

grid.DataSource property is null!!! That way doesn't work.
 
My grid's datasource is an ObjectDataSource connected to a typed dataset.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 14 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100