When I already enabled paging and sorting for a GridView Control? Here's my aspx code:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True">
</asp:GridView>
<asp:S开发者_JAVA技巧qlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:plantsSQLConnectionString %>"
SelectCommand="SELECT * FROM [Plants]"></asp:SqlDataSource>
I do the query and DataBind in Page_Load() and the data were displayed properly. I was expecting GridView to handle the paging and sorting for me but when I tried to page or sort, the page crashed and I was told to provide the Sorting and PageIndexChanging handlers. What did I do wrong? Thanks.
Here's my code behind. You can assume GetData works because I see the records displayed properly.
protected void Page_Load(object sender, EventArgs e)
{
// Declare the query string.
String queryString = "Select * from plants";
// Run the query and bind the resulting DataSet
// to the GridView control.
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
Additional details: If I don't do code behind and do the query in the aspx file then GridView can sort and page automatically. [Meaning I used the wizard and connected the GridView to the datasource in the aspx file resulting in a DataSource ID being associated with the GridView control.] How can it do the paging and sorting in this case and not when code is moved to PageLoad? Thanks for any insight.
The GridView does yet not know how to sort if the user clicks on a column. Therefore you need to provide the SortExpression for the sortable columns. Also you might want to provide extra functionality to sort ascending/descending if user clicked on the same column twice. Therefore you need to store the current SortExpression+SortDirection somewhere f.e. in ViewState.
Some considerations why and when sorting might not be working automatically:
When a data source control that supports sorting is bound to the GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic sorting functionality. When the GridView control is bound to a data source by setting the DataSource property programmatically, you must provide the sorting functionality by using the Sorting event.
Here are further informations on Sorting: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.allowsorting.aspx
Some considerations why and when paging might not be working automatically:
If the data source does not support paging directly and does not implement the ICollection interface, the GridView control cannot page. For example, if you are using a SqlDataSource control and have set its DataSourceMode property to DataReader, the GridView control cannot implement paging.
Here are more informations on why the GridView might not support paging directly: http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx
Edit: Remember to check for !
Page.IsPostback in Page_Load before you databind the GridView! Otherwise the GridView will get the old data/paging/sorting etc. on postbacks.
精彩评论