|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Gridview custom sort - works but always sorts ascendingI have a gridview in which I am doing a custom sort for the lastname of a person. When that column is clicked, I would like to additionally sort the firstname ascending as such: protected void gvClients_Sorting(object sender, GridViewSortEventArgs e) { if (e.SortExpression == "szLastName") { e.SortExpression +=", szFirstName"; } } This works, but e.SortDirection is always Ascending. How can I get this to sort descending? Thanks, David If you want the firstName sort direction to match lastName then you'll
need to test for SortDirection and change the SortExpression accordingly. I haven't tested code below but it should be something like: if (e.SortDirection == SortDirection.Ascending && e.SortExpression =="szLastName") { e.SortExpression += ", szFirstName"; } else { e.SortExpression += ", szFirstName DESC"; } Thanks for your response but I don't think I was clear enough. The problem
is not that I want to change the sort order of szFirstName, but that e.SortDirection is always Ascending when I modify e.SortExpression. If I comment this out, e.SortDirection alternates between ASC and DESC as it should. <CaffieneR***@gmail.com> wrote in message Show quoteHide quote news:1142516071.889893.220690@p10g2000cwp.googlegroups.com... > If you want the firstName sort direction to match lastName then you'll > need to test for SortDirection and change the SortExpression > accordingly. > I haven't tested code below but it should be something like: > if (e.SortDirection == SortDirection.Ascending && e.SortExpression > =="szLastName") > { > e.SortExpression += ", szFirstName"; > } > else > { > e.SortExpression += ", szFirstName DESC"; > } > I see, so you're saying that the sort direction is no longer
automatically toggling after modifying the SortExpression. That means you'll need to do it yourself. if (e.SortDirection == SortDirection.Ascending && e.SortExpression.StartsWith("szLastName")) { e.SortExpression = " szLastName ASC, szFirstName ASC"; 'Manually toggle SortDirection; e.SortDirection = SortDirection.Descending; } else if (e.SortDirection == SortDirection.Descending && e.SortExpression.StartsWith("szLastName")) { e.SortExpression = " szLastName DESC, szFirstName DESC"; 'Manually toggle SortDirection; e.SortDirection = SortDirection.Ascending; } Good Luck.
GridView w/ ObjectDataSource w/ Business object layer
GridView, ObjectDataSOurce, and enum parameter for select Web UserControl (ascx) and UrlProperty attribute Cant get user-entered values in GridView Scrolling DG w/a Fixed Header Datalist formatting questions Adding client side code to RadioButtonList control item??? DataGrid1.DataSource = ds.Tables(2) Treeview SelectedNodeChanged event not firing GridView custom control and post back issues |
|||||||||||||||||||||||