How to make asp: gridview sorted?

I have an asp:GridView control that I set for the AllowSorting="True" property:

 <asp:GridView ID="gridUsers" runat="server" PageSize="100" ShowHeaderWhenEmpty="True" Width="100%" AllowSorting="True" onrowcreated="gridUsers_RowCreated" onsorting="gridUsers_Sorting"> </asp:GridView> 

At design time, the grid looks sortable:

enter image description here

But at runtime, only the middle column is sorted:

enter image description here

How to make asp:GridView sorted in ASP.NET?


Note For asp:GridView with AllowSorting , a Sorting event handler must be present:

 protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e) { //asp:GridView will throw an exception if a Sorting event handler isn't present } 

Update : I realized what is especially important in the Description column. This is the only column whose display name is correct from the database as is. The rest of the columns I have to fix the display name :

 protected void gridUsers_RowCreated(object sender, GridViewRowEventArgs e) { e.Row.Cells[0].Visible = false; //UserGUID e.Row.Cells[1].Text = "User name"; e.Row.Cells[2].Text = "Full name"; //3=Description e.Row.Cells[4].Text = "E-mail"; e.Row.Cells[5].Text = "Active"; e.Row.Cells[5].Visible = false; e.Row.Cells[6].Text = "Account type"; } 

Now I just need to figure out the hard part; and sort the columns.

+4
source share
2 answers

this code will definitely help you:

In the GridView, create the AllowSorting = "True" property and in the GridView columns do the following

 <asp:BoundField DataField="UserName" HeaderText="User Name" SortExpression="UserName" /> <asp:BoundField DataField="FullName" HeaderText="Full Name" SortExpression="FullName" /> 

and also use the following encoding:

 protected void gv_Sorting(object sender, GridViewSortEventArgs e) { try { string sortExpression = e.SortExpression; ViewState["z_sortexpresion"] = e.SortExpression; if (GridViewSortDirection == SortDirection.Ascending) { GridViewSortDirection = SortDirection.Descending; SortGridView(sortExpression, "DESC"); } else { GridViewSortDirection = SortDirection.Ascending; SortGridView(sortExpression, "ASC"); } } catch (Exception ex) { return ex.Message.ToString(); } } public SortDirection GridViewSortDirection { get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection)ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } private void SortGridView(string sortExpression, string direction) { DTSorting = new DataView(DTSorting, "", sortExpression + " " + direction, DataViewRowState.CurrentRows).ToTable(); gv.DataSource = DTSorting; gv.DataBind(); } public DataTable DTSorting { get { if (ViewState["Sorting"] != null) { return (DataTable)ViewState["Sorting"]; } else return null; } set { ViewState["Sorting"] = value; } } 

Here "DTSorting" is the DataTable with which you bind the GridView "gv".

+5
source
 protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e) { DataTable dt = Session["SortedTable"] as DataTable; if (dt != null) { //Sort the data. dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression); gridUsers.DataSource = Session["SortTable"]; gridUsers.DataBind(); } } 

Adapted from this msdn example :

+1
source

All Articles