Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am trying to sort my grid view. I checked other posts as well but my code does not run for some reason. Can someone please tell me what i am doing wrong here. Any help will be greatly appreciated. Thank you.

What I have tried:

protected void grdCustomers_Sorting(object sender, GridViewSortEventArgs e)

{


}


<asp:GridView ID="gridviewCustomers" runat="server" AllowPaging="false" AllowSorting="True" CellPadding="4" OnSorting="grdCustomers_Sorting" DataKeyNames="CustomerID" OnSelectedIndexChanged="grdCustomers_SelectedIndexChanged">
Posted
Updated 6-Jul-17 9:35am
v7

1 solution

I cannot see where you are actually applying the sort.
If your event handler grdCustomers_Sorting is not firing then check the GridView has the property
C#
AllowSorting=True


If your event handler is firing then it is likely to be because you have not applied the sort from e.SortExpression. Review the documentation at;
GridView.Sorting Event (System.Web.UI.WebControls)[^]

Kind Regards
 
Share this answer
 
Comments
Member 13289364 3-Jul-17 17:42pm    
I actually do have AllowSorting=True if you look at the code at the very bottom.I did refer to the documentation you mentioned but it is giving me the following error.
Can you please help me fix this issue. Thank you.

System.Web.UI.WebControls.SqlDataSource' does not contain a definition for 'DefaultView' and no extension method 'DefaultView' accepting a first argument of type 'System.Web.UI.WebControls.SqlDataSource' could be found (are you missing a using directive or an assembly reference?
an0ther1 4-Jul-17 1:38am    
Sorry, missed that first time I viewed it.
Where you populating dscCustomers? In the code behind? If it is a SQL Data Source then you will be unable to sort it unless you re-set the Command Text to include your sorting and reload the data.
EDIT: I read up some more on the DataSource control & you can sort it but you must set the DataSourceMode property to DataSet. The DataSet will then contain one (or more) DataTables from your query. Refer https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.datasourcemode.aspx

DefaultView is a property of a DataTable, refer; https://msdn.microsoft.com/en-us/library/340c39ak(d=hv.2,v=vs.110).aspx

If you have a small table you would typically query the DB when the page loads & then add the DataTable to the Session object as shown in the example.
When the event handler fires you would retrieve the DataTable from the Session object and apply the sorting as per example.
If it was a large table & you didn't want to add it to the Session object you would reload it from SQL and re-bind to the GridView

Hope this helps

Member 13289364 5-Jul-17 21:55pm    
I tried everything but it's not working for me. My sorting is working for all other pages but this one. The data is coming from SQL Data Source and i have included it above. I just updated my codes. Can you please help me figure out what i am missing here. I have been reading documentations and other posts but nothing is working for me. Thank you.
an0ther1 6-Jul-17 0:14am    
Debug your code & see what DataSourceMode of you DataSource control is. If it is DataReader - the default - then you need to change it to DataSet where you define your SqlDataSource - Should be as easy updating your code as per below;
<asp:SqlDataSource ID="dscCustomers" runat="server" DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:ACCDBConnection %>"
ProviderName="<%$ ConnectionStrings:ACCDBConnection.ProviderName %>"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900