Gridview Paging and Sorting
-
Hi, When manually binding an SqlDataSource, sorting and paging does not work on the GridView. Is there some kind of settings or code to make the sorting and paging work? SqlDataSource1.SelectCommand() = sSQL GridView1.DataSource = SqlDataSource1 GridView1.DataBind() Note that when the SqlDataSource1 and the GridView is bind at design, the sort and paging work fine. Thank you.
-
Hi, When manually binding an SqlDataSource, sorting and paging does not work on the GridView. Is there some kind of settings or code to make the sorting and paging work? SqlDataSource1.SelectCommand() = sSQL GridView1.DataSource = SqlDataSource1 GridView1.DataBind() Note that when the SqlDataSource1 and the GridView is bind at design, the sort and paging work fine. Thank you.
Greetings satishkumarnk, You will need to implement the following two events:
1 - PageIndexChanging
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { // This line tells the grid which page to go to // Have a look at the GridViewPageEventArgs object // to get more insight on what information it contains. GridView.PageIndex = e.NewPageIndex; // Simply rebind your grid. GridView.DataBind(); }
2 - Sorting
protected void GridView_Sorting(object sender, GridViewSortEventArgs e) { // We will append this string later with the direction // for now lest just add the column name the user clicked on string sqlScript = "Select * From MyTable Order By " + e.SortExpression; // Test for the direction... if (e.SortDirection == SortDirection.Ascending) { // Append the ASC keyword to your script sqlScript += " ASC"; } else { // The SortDirection enumeration is binary so an else block will suffice sqlScript += " DESC"; } // Rerun your sql script to retrieve the newly sorted data... // bind the gridview GridView.DataBind(); }
And thats it... Happy implementing! :)
Fernando Mendes Senior .NET Developer, Architect