Pagination in ASP.Net DataGrid
Quick steps to work out pagination in your datagrid.. Go to your data grid properties... Set AllowPaging property to true... Set the PageSize to your desired page size... Now go to events section in the properties... You have to override PageIndexChanged Event... In the event handler... Write code similar to the one below:
private void dgdImages_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
//Change the current page index to the new one
dgdImages.CurrentPageIndex = e.NewPageIndex;
//Update your data if required
dgdImages.DataSource = CreateDataSource();
//bind the data again
That's it... Your datagrid does not need to make your page long scrollable now... Do note though that if you change the data in the background and the new data does not contain as many pages as your current page index now you might land up getting exceptions... So make sure that you do the necessary work there...
PS: I was driving my car today, trying to rush home from work, had to do some groceries and go to library to return books and read a martin fowler's new design pattern discussion and cook and have dinner and then go to photocopy some legal documents... Oh!! I felt... 24 hours are just not enough sometime... Then I saw a guy casually walking his dog and listening to his discman... I passed by and then pulled over... Thought for while...where was I actually going wrong, why was his life so different and nicer than mine... Why life has to be unjust, just to me.. And you know what I did next... Drove to Starbucks coffee house... Took a nice tall mocha and spent 30 minutes thinking of my own design pattern... I am paying $2 fine on the library books now and I spent $5 extra at the coffee house but then Ahh!! what a realization... My life was not much different than his, it was just in the way I looked at it...