Better late than never?
Some supplement for China's offer, which is basically the right one.
In truth, you have to deal with sorting by the gridView_Sorting event. There is no need for a DataBind () GridView before, for example, in the Page_Load event. There you should call the GridView.Sort () method instead of .DataBind (). Here's how to do it:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Me.gridView.Sort(Request.QueryString("sortExpression"), Request.QueryString("sortDirection")) End If End Sub
Next, consider the gridView_Sorting event.
There you need to push the data source to the correct sort. GridView alone does not process (in this case, at least).
Protected Sub gridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gridView.Sorting If IsPostBack Then e.Cancel = True Dim sortDir As SortDirection = SortDirection.Ascending If e.SortExpression = Me.Q_SortExpression And Me.Q_SortDirection = SortDirection.Ascending Then sortDir = SortDirection.Descending End If RedirectMe(e.SortExpression, sortDir) Else Dim sortExpr As String = e.SortExpression + " " + IIf(e.SortDirection = SortDirection.Ascending, "ASC", "DESC") Dim dv As System.Data.DataView = Me.dsrcView.Select(New DataSourceSelectArguments(sortExpr)) Me.gridView.DataSource = dv Me.gridView.DataBind() End If End Sub
There is no need to encode any sorting function in the data source, for example, passing sorting parameters to a stored procedure. All sorting is done in the above code snippets.
Itβs also good that gridView.EnableViewState is switched to False, which makes the page much easier for network traffic and for the browser. This can be done because the grid is completely recreated whenever the page is sent back.
Have a nice day!
Martin
source share