我有一个asp:GridView
控制,我已经设置AllowSorting="True"
属性上:如何让asp:GridView排序?
<asp:GridView ID="gridUsers" runat="server" PageSize="100" ShowHeaderWhenEmpty="True"
Width="100%" AllowSorting="True" onrowcreated="gridUsers_RowCreated"
onsorting="gridUsers_Sorting">
</asp:GridView>
在设计时,网格看起来排序:
但在运行时只有中间一列是可分拣:
如何在ASP.NET中对asp:GridView
进行排序?
注意:该asp:GridView
与AllowSorting
需要一个Sorting
事件处理程序存在:
protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e)
{
//asp:GridView will throw an exception if a Sorting event handler isn't present
}
更新:我意识到什么特别之处说明列。它是显示名称从数据库原样显示的唯一列,原样为。其余列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";
}
现在我只需要找出棘手的部分;并使列可以排序。
我不得不重新设计所有使用“早期”绑定,而不是“迟到”绑定(因此在答案中使用'asp:BoundField')。 GridView扼流器如果在运行时绑定它。 –