asp.net - 如何制作asp :GridView sortable?

标签 asp.net sorting webforms

我有一个 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>

在设计时,网格看起来是可排序的:

enter image description here

但在运行时只有中间列是可排序的:

enter image description here

如何在 ASP.NET 中使 asp:GridView 可排序?

<小时/>

注意:带有 AllowSortingasp:GridView 需要 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";
 }

现在我只需要弄清楚棘手的部分;并使列可排序。

最佳答案

这段代码肯定会对你有帮助:

在 GridView 中,将属性 AllowSorting = "True"并在 GridView 列中给出如下

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

并使用以下编码:

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;
    }
}

这里,考虑“DTSorting”是绑定(bind) GridView“gv”的 DataTable。

关于asp.net - 如何制作asp :GridView sortable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10987515/

相关文章:

c# - DetailsView 的事件 "ItemUpdating"中的 OldValues 集合始终为空

asp.net - 只使用一次的功能应该放在 UserControl 中吗?

asp.net - ViewState 支持的属性不适用于 List<T>

c# - 如何最好地响应开放 HTTP 范围请求

javascript - 计算对象属性给出与点表示法不同的结果

python - 如何在 Pandas 数据框中按行值对日期时间列进行排序?

cocoa - 如何使用 sortDescriptors 在 Swift 中对 NSTableView 进行排序?

javascript - 如何在 Javascript 或 JQuery 中获取 Gridview 中的选定行

asp.net - 如何检测 ASP.net 应用程序中的 SqlServer 连接泄漏?

c# - 几分钟不活动后,使用 SSL 时出现错误 413 Request Entity Too Large