【发布时间】:2012-06-14 19:05:34
【问题描述】:
我有一个asp:GridView 控件,我已将AllowSorting="True" 属性设置为:
<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";
}
现在我只需要找出棘手的部分;并使列可排序。
【问题讨论】: