Asked by:
Autosorting in Gridview

Question
-
User944339287 posted
Hi guys.. I have set AllowSorting = True in GridView. How can i sort ID or Name in ascending order and descending order by clicking the header of Gridview?
HTML<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False" DataKeyNames="CID" AllowSorting="True" AllowPaging="True" PageSize="25"> <Columns> <asp:BoundField DataField="ID" HeaderText="Customer ID" /> <asp:BoundField DataField="name" HeaderText="Name" /> </Columns> </asp:GridView>
CODE BEHIND
Private Sub BindGrid() DT = customer.return_customer("All","Customer") Me.GridView.DataSource = DT.DefaultView Me.GridView.DataBind() End Sub
Tuesday, May 8, 2018 1:21 PM
All replies
-
User475983607 posted
The GridView documentation explains how to do this with source code. Always start by reading the openly published reference documentation.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting(v=vs.110).aspx
Tuesday, May 8, 2018 1:51 PM -
User3690988 posted
If your GridView is bound to a DataTable, you will have to code your own sorting routine. Here is an example: GridView Sorting Event
Tuesday, May 8, 2018 1:54 PM -
User283571144 posted
Hi kengkit,
According to your description, I suggest you could firstly add SortExpression property for each column you would to use for sorting:
SortExpression property indicates the expression that should be used to sort the data when that field's sorting header link is clicked
Then I suggest you could convert the datatable to dataview and set the datatable's sort attribute.
More details, you could refer to below codes:
ASPX:
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" AllowSorting="true" OnSorting="GridView1_Sorting" > <Columns> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Product_Name" HeaderText="Product_Name" SortExpression="Product_Name" /> <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> </Columns> </asp:GridView> <br /> </div>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { var data = DataTableExmaple.LoadData(); GridView1.DataSource = data; GridView1.DataBind(); } } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { //Using DataView for sorting DataTable's data var data = DataTableExmaple.LoadData(); DataView view = DataTableExmaple.LoadData().DefaultView; view.Sort = String.Format("{0} {1}", e.SortExpression, GetSortingDirection()); GridView1.DataSource = view; GridView1.DataBind(); } protected string GetSortingDirection() { if (ViewState["SortDirection"] == null) ViewState["SortDirection"] = "ASC"; else if (ViewState["SortDirection"].ToString() == "ASC") ViewState["SortDirection"] = "DESC"; else ViewState["SortDirection"] = "ASC"; return ViewState["SortDirection"].ToString(); }
Result:
Best Regards,
Brando
Friday, May 11, 2018 8:41 AM