i have an asp:GridView
control, which i've set the AllowSorting="True"
property on:
<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:
But at runtime only the middle column is sortable:
How do i make an asp:GridView
sortable in ASP.NET?
Note: The asp:GridView
with AllowSorting
requires a Sorting
event handler to 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's special about the Description column. It's the only column whose display name is correct from the database as-is. The remaining columns i have to fix the display name to be presentable:
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 have to figure out the tricky part; and make columns sortable.
Best Answer
this code will definitely help you:
In the GridView, Make the Property AllowSorting = "True" and in the GridView Columns give like this
and also use the below coding:
Here, Consider "DTSorting" is the DataTable by which you binds the GridView "gv".