How to enable Paging and Sorting on ASP.NET 4.0 GridView programmatically?

You need to bind your GridView every time you change page.

Up vote 1 down vote favorite share g+ share fb share tw.

I am using ASP. NET 4.0 with C# (Visual Web Developer 2010 Express). I have successfully managed to implement a simple GridView bound to a stored procedure data source using declarative ASP.

NET code as shown here: " ProviderName="" SelectCommand="usp_GetTrades" SelectCommandType="StoredProcedure"> It works great including paging and sorting. I want to remove the SqlDataSource and use code-behind (I'm trying to put database access code in one place). So far I have this in my code-behind: protected void Page_Load(object sender, EventArgs e) { if (!this.

IsPostBack) { grdTrades. SelectedIndex = 0; DBUtil DB = new DBUtil(); grdTrades. DataSource = DB.GetTrades(); grdTrades.

DataKeyNames = new string { "tradeId" }; grdTrades.DataBind(); } } // this is needed otherwise I get "The GridView 'grdTrades' fired event PageIndexChanging which wasn't handled. " void grdTrades_PageIndexChanging(Object sender, GridViewPageEventArgs e) { grdTrades. PageIndex = e.

NewPageIndex; grdTrades.DataBind(); } My declarative code now looks like: The problem is when I click on a page number the page becomes blank. I would also like to implement sorting but would like to get the paging working first. Please help.

Thanks c# asp.net gridview link|improve this question asked Nov 3 '10 at 8:57Mark Allison82111030 89% accept rate.

You need to bind your GridView every time you change page. For example: void grdTrades_PageIndexChanging(Object sender, GridViewPageEventArgs e) { grdTrades. DataSource = DB.GetTrades(); grdTrades.

PageIndex = e. NewPageIndex; grdTrades.DataBind(); } My advice would be to store your results from DB.GetTrades() in the ViewState (or Cache) so you don't need to go to the database everytime you change page. Sorting can become quite difficult when doing this, though.

You can always use an ObjectDataSource instead of a SqlDatasource. You can then point your ObjectDataSource to look at your DB.GetTrades() function. Sorting and Paging will work automatically.

Hope that helps.

Actually sorting will only work automatically on an ObjectDataSource if you are returning a DataSet, I believe (I could be wrong). If you are returning a List then you will need to create your own class that inherits from the IComparere class. – Jamie Nov 3 '10 at 13:53 Great thanks Jamie, works fine.

I would like to investigate the ObjectDataSource method - if you have time would you be able to provide a quick example of how to do that? It sounds like a better performing and more sensible way of doing it. – Mark Allison Nov 3 '10 at 14:08 1 I'm not sure if you are using business objects but this may help you get started: msdn.microsoft.com/en-us/library/9a4kyhc....

– Jamie Nov 3 '10 at 14:48 ObjectDataSource looks like the way to go. Very nice, and thanks for the help. – Mark Allison Nov 3 '10 at 15:55.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions