Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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

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 
...
 
Share this answer
 
Hi You need to Set Total Records when you assign DataSource to the Grid.

e.g.
C#
int TotalRecord = dt.Rows.Count(); //This is total number of records in gridview
GridView1.DataSource = dt;
GridView1.DataBind();



Thanks,
Imdadhusen
 
Share this answer
 
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
 
Share this answer
 
v2
Comments
rajivpande86 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 19-Jul-13 23:18pm    
Gridview1.Rows.Count will return the number of rows in Gridview
Gridview1.Rows.Count will return the number of rows in Gridview
 
Share this answer
 
Comments
CHill60 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.
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.
 
Share this answer
 
grid.DataSource property is null!!! That way doesn't work.

My grid's datasource is an ObjectDataSource connected to a typed dataset.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900