ppittle
asked on
GridView - Pagination - Total Record Count
I am databinding a GridView to a DataTable, via an ObjectDataSource. I have enabled pagination and would like to display pagination data on the GUI as follows:
Displaying [Index of 1st Record on GridView.Page] - [Index of last Record on GridView.Page] of [GridView's total record count] records.
I can calculate the Index of the 1st record:
((this.GridView1.PageIndex + 1) * this.GridView1.PageSize) - (this.GridView1.PageSize - 1);
I can calcualte the Index of the Last Record displayed on the current Page:
((this.GridView1.PageIndex + 1) * this.GridView1.PageSize);
How can I calculate the total record count?
Thanks,
PJ
Displaying [Index of 1st Record on GridView.Page] - [Index of last Record on GridView.Page] of [GridView's total record count] records.
I can calculate the Index of the 1st record:
((this.GridView1.PageIndex
I can calcualte the Index of the Last Record displayed on the current Page:
((this.GridView1.PageIndex
How can I calculate the total record count?
Thanks,
PJ
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yeah, sorry, i've just added the pagination and noticed that it's only rows on the actual page.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all the feed back! After reviewing your comments and tinkering a bit I came up with this, which works:
protected void AllStoriesGridView_DataBou nd(object sender, EventArgs e)
{
PaginationRecordCountLabel .Visible = (AllStoriesGridView.Rows.C ount > 0);
string recordCountFormatString = "Displaying records {0}-{1} out of {2}.";
ObjectDataSource ods = (ObjectDataSource)AllStori esGridView .DataSourc eObject;
int totalRowsInDataSource = (ods.Select() as DataView).Count;
int firstRecordOnPageIndex = ((AllStoriesGridView.PageI ndex + 1) * AllStoriesGridView.PageSiz e) - AllStoriesGridView.PageSiz e + 1;
int lastRecordOnPageIndex;
if (((AllStoriesGridView.Page Index + 1) * AllStoriesGridView.PageSiz e) > totalRowsInDataSource)
{
lastRecordOnPageIndex = totalRowsInDataSource;
}
else
{
lastRecordOnPageIndex = ((AllStoriesGridView.PageI ndex + 1) * AllStoriesGridView.PageSiz e);
}
PaginationRecordCountLabel .Text = string.Format(recordCountF ormatStrin g,
firstRecordOnPageIndex,
lastRecordOnPageIndex,
totalRowsInDataSource);
}
cdebel and urir10 I tried this out too:
protected void AllStoriesDataSource_Selec ted(object sender, ObjectDataSourceStatusEven tArgs e)
{
int totalRowsInDataSource = (e.ReturnValue as DataTable).Rows.Count;
}
Which is similiar to what the two of you suggested. However, this doesn't take into account any filtering done on the ObjectDataSource, which, granted, wasn't part of my original problem description. It does, however, return the number of rows being returned from the DB.
I also looked at e.AffectedRows (where e is ObjectDataSourceStatusEven tArgs), and it always returned -1 for some reason. Might be some odd difference between ObjectDataSources and SqlDataSources.
Regardless, thank you both for the assistance.
PJ
protected void AllStoriesGridView_DataBou
{
PaginationRecordCountLabel
string recordCountFormatString = "Displaying records {0}-{1} out of {2}.";
ObjectDataSource ods = (ObjectDataSource)AllStori
int totalRowsInDataSource = (ods.Select() as DataView).Count;
int firstRecordOnPageIndex = ((AllStoriesGridView.PageI
int lastRecordOnPageIndex;
if (((AllStoriesGridView.Page
{
lastRecordOnPageIndex = totalRowsInDataSource;
}
else
{
lastRecordOnPageIndex = ((AllStoriesGridView.PageI
}
PaginationRecordCountLabel
firstRecordOnPageIndex,
lastRecordOnPageIndex,
totalRowsInDataSource);
}
cdebel and urir10 I tried this out too:
protected void AllStoriesDataSource_Selec
{
int totalRowsInDataSource = (e.ReturnValue as DataTable).Rows.Count;
}
Which is similiar to what the two of you suggested. However, this doesn't take into account any filtering done on the ObjectDataSource, which, granted, wasn't part of my original problem description. It does, however, return the number of rows being returned from the DB.
I also looked at e.AffectedRows (where e is ObjectDataSourceStatusEven
Regardless, thank you both for the assistance.
PJ
ASKER
Note: I posted the solution that worked in my case (ObjectDataSources). cdebel and urir10's comment's are only marked as solution in order to award points for their assistance.
Open in new window