如何制作asp:GridView可排序?

时间:2012-06-11 21:05:37

标签: 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排序?


注意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
}

更新:我意识到描述列的特殊之处。它是数据库 as-is 中显示名称正确的唯一列。其余列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";
 }

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

2 个答案:

答案 0 :(得分:6)

此代码肯定会对您有所帮助:

在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”是您通过它绑定GridView“gv”的DataTable。

答案 1 :(得分:1)

protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dt = Session["SortedTable"] as DataTable;

    if (dt != null)
    {

      //Sort the data.
      dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
      gridUsers.DataSource = Session["SortTable"];
      gridUsers.DataBind();
    }
}

改编自msdn example