使用gridview asp.net进行排序和分页

时间:2009-03-31 19:11:58

标签: asp.net gridview sorting paging

我正在尝试使用gridview手动排序和分页,但没有成功。

问题在于,当用户单击要排序的列时,它会对该页面进行排序,但不会对gridview后面的数据源(数据视图)进行排序。因此,当他们进入不同的页面时,他们的排序就会丢失。我正在寻找一种实际排序gridview背后的数据源的排序。以下是我到目前为止的情况:

protected void GridView_OnSort(object sender, GridViewSortEventArgs e)
    {
        String sortExpression = e.SortExpression;

        if (GridViewSortDirection == SortDirection.Ascending)
        {
            DataView myDataView = new DataView(mybll.GetItemsOrdered());
            myDataView.Sort = sortExpression + " DESC";
            GridView.DataSource = myDataView;
            GridView.DataBind();
        }
        else
        {
            DataView myDataView = new DataView(mybll.GetItemsOrdered());
            myDataView.Sort = sortExpression + " ASC";
            GridView.DataSource = myDataView;
            GridView.DataBind();
        }
    }

任何帮助将不胜感激。感谢。

5 个答案:

答案 0 :(得分:54)

将您的排序顺序保存在ViewState中。

private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";

public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;

        return (SortDirection) ViewState["sortDirection"];                
    }
    set { ViewState["sortDirection"] = value; } 
}

protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;

    if (GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView(sortExpression, DESCENDING);
    }
    else
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView(sortExpression, ASCENDING); 
    }   

}

private void SortGridView(string sortExpression,string direction)
{
    //  You can cache the DataTable for improving performance
    DataTable dt = GetData().Tables[0]; 

    DataView dv = new DataView(dt); 
    dv.Sort = sortExpression + direction;         

    GridView1.DataSource = dv;
    GridView1.DataBind();         
}

为什么您不想使用现有的排序功能?您可以随时自定义它。

MSDN

Sorting Data in a GridView Web Server Control

以下是自定义的示例:

http://www.netomatix.com/development/GridViewSorting.aspx

答案 1 :(得分:5)

<asp:GridView 
    ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="True" onsorting="GridView1_Sorting" EnableViewState="true"> 
    <Columns>
        <asp:BoundField DataField="bookid" HeaderText="BOOK ID"SortExpression="bookid"  />
        <asp:BoundField DataField="bookname" HeaderText="BOOK NAME" />
        <asp:BoundField DataField="writer" HeaderText="WRITER" />
        <asp:BoundField DataField="totalbook" HeaderText="TOTALBOOK" SortExpression="totalbook"  />
        <asp:BoundField DataField="availablebook" HeaderText="AVAILABLE BOOK" />
    </Columns>
</asp:GridView>

代码背后:

protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            string query = "SELECT * FROM book";
            DataTable DT = new DataTable();
            SqlDataAdapter DA = new SqlDataAdapter(query, sqlCon);
            DA.Fill(DT);

            GridView1.DataSource = DT;
            GridView1.DataBind();
        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {

        string query = "SELECT * FROM book";
        DataTable DT = new DataTable();
        SqlDataAdapter DA = new SqlDataAdapter(query, sqlCon);
        DA.Fill(DT);

        GridView1.DataSource = DT;
        GridView1.DataBind();

        if (DT != null) {
            DataView dataView = new DataView(DT);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridView1.DataSource = dataView;
            GridView1.DataBind();
        }
    }

    private string GridViewSortDirection {
        get { return ViewState["SortDirection"] as string ?? "DESC"; }
        set { ViewState["SortDirection"] = value; }
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection) {
        switch (GridViewSortDirection) {
            case "ASC":
                GridViewSortDirection = "DESC";
                break;

            case "DESC":
                GridViewSortDirection = "ASC";
                break;
        }

        return GridViewSortDirection;
    }
}

答案 2 :(得分:0)

我找到了一种更简单的方法,它允许你仍然使用标准gridview的内置排序/分页......

创建2个标签。将它们设置为visible = false。我打电话给我的lblSort1和lblSortDirection1

然后编写2个简单事件...页面排序,写入不可见标签的文本,页面索引更改,使用它们...

Private Sub gridview_Sorting(sender As Object, e As GridViewSortEventArgs) Handles gridview.Sorting
lblSort1.Text = e.SortExpression
lblSortDirection1.Text = e.SortDirection
End Sub

Private Sub gridview_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gridview.PageIndexChanging
    gridview.Sort(lblSort1.Text, CInt(lblSortDirection1.Text))
End Sub

这比使用全局变量有点麻烦,但我发现使用asp特别是全局变量是不可靠的......

答案 3 :(得分:0)

更简单的方式......:

    Dim dt As DataTable = DirectCast(GridView1.DataSource, DataTable)
    Dim dv As New DataView(dt)

    If GridView1.Attributes("dir") = SortDirection.Ascending Then
        dv.Sort = e.SortExpression & " DESC" 
        GridView1.Attributes("dir") = SortDirection.Descending

    Else
        GridView1.Attributes("dir") = SortDirection.Ascending
        dv.Sort = e.SortExpression & " ASC"

    End If

    GridView1.DataSource = dv
    GridView1.DataBind()

答案 4 :(得分:0)

Tarkus的答案很有效。但是,我建议用SESSION替换VIEWSTATE。

当前页面的VIEWSTATE仅在当前页面发回自身时才有效,一旦用户被重定向到另一个页面就会消失。 SESSION持续排序顺序不仅仅是当前页面的回发。它会在整个会话期间持续存在。这意味着用户可以浏览其他页面,当他回到给定页面时,他上次使用的排序顺序仍然存在。这通常更方便。

还有其他方法,例如保留用户个人资料。

我推荐这篇文章是为了更好地解释ViewState以及它如何与网页的生命周期一起使用:https://msdn.microsoft.com/en-us/library/ms972976.aspx

要理解VIEWSTATE,SESSION和其他持久化变量的方法之间的区别,我建议这篇文章:https://msdn.microsoft.com/en-us/library/75x4ha6s.aspx