asp.net gridview排序自定义数据源

时间:2012-09-21 21:44:35

标签: asp.net gridview

我有一个正在填充的网格视图。现在我想启用排序。我已经完成了所有必需的代码 - 即在标记上启用排序并在用户排序时提供要调用的事件。

它失败的排序事件 - 我尝试了一些谷歌的实施,但我不太确定。基本上我的核心是说我需要向服务器提供新的查询,具体取决于用户想要排序的列和ASC或DESC?如果是这样的话,听起来有很多工作......更多的疑问。

感谢 DAMO

绑定网格后面的代码

 // Load the main homepage data to the grid
                    FAServices fServices = new FAServices(sConn);
                    FAAuditOverallStatusLatest fAuditOverallStatusLatest = new FAAuditOverallStatusLatest(sConn);
                    GridViewMain.DataSource = fAuditOverallStatusLatest.getAuditOverallStatusLatest();
                    GridViewMain.DataBind();

排序代码

protected void GridViewMain_Sorting(object sender, GridViewSortEventArgs e)
{

    // Switch statements required here along with Query for each column i have in the grid




}

网格标记

<asp:GridView ID="GridViewMain" OnRowDataBound="GridViewMainRowDataBound" OnPageIndexChanging="GridViewMain_PageIndexChanging"
                                        runat="server"  AllowPaging="True" PageSize="50" PagerSettings-Position="TopAndBottom"
                                        CssClass="mGrid"
                                        PagerStyle-CssClass="pgr"
                                        AlternatingRowStyle-CssClass="alt data-row"
                                        OnRowCreated="GridViewMain_RowCreated"                          
                                        RowStyle-CssClass="data-row"                                        
                                        AllowSorting="True"
                                        OnSorting="GridViewMain_Sorting"
                                        >
                                     </asp:GridView>

2 个答案:

答案 0 :(得分:1)

  

我的核心是说我需要提供新的查询   服务器取决于用户想要排序的列和ASC或   DESC也呢?如果是这样的话,听起来有很多工作......更多的疑问。

是的,你是对的。您需要再次查询数据源以应用新排序。但最后一句话是不正确的。您只需要更改sql的ORDER BY(或者您用来订购DataSource的任何内容)。你可以使用一个ViewState变量,order-column和direction(你需要在回发中保留它,因此是ViewState)。我会告诉你一个例子:

首先,您需要设置SortExpression

<asp:TemplateField HeaderText="Name" SortExpression="Name">
    <ItemTemplate>
        <a href='sometest.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "TestID").ToString()%>'><%# DataBinder.Eval(Container.DataItem, "Type").ToString()%></a>
    </ItemTemplate> 
</asp:TemplateField>    
<asp:TemplateField HeaderText="Address" SortExpression="Address">
    <ItemTemplate>
                <div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
    </ItemTemplate> 
</asp:TemplateField>            

在代码隐藏中,您可以将当前SortExpressionSortDirection存储在ViewState中:

private string SortExpression {
    get {
        if(String.IsNullOrWhiteSpace((String)ViewState["SortExpression"])
            ViewState["SortExpression"] = "Name ASC";

        return (String)ViewState["SortExpression"];
    }
    set { ViewState["SortExpression"] = value; }
}

这是Sorting事件处理程序。请注意BindGrid是设置DataSource并调用GridView.DataBind

的方法
protected void theGrid_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
    string currentSortColumn = null;
    string currentSortDirection = null;
    currentSortColumn = this.SortExpression.Split(' ')[0];
    currentSortDirection = this.SortExpression.Split(' ')[1];

    if (e.SortExpression.Equals(currentSortColumn)) {
        //switch sort direction
        switch (currentSortDirection.ToUpper()) {
            case "ASC":
                this.SortExpression = currentSortColumn + " DESC";
                break;
            case "DESC":
                this.SortExpression = currentSortColumn + " ASC";
                break;
        }
    } else {
        this.SortExpression = e.SortExpression + " ASC";
    }

    //load the data with this SortExpression and DataBind the Grid
    BindGrid();
}

答案 1 :(得分:1)

我有一个方法更简单的几行:(在vb.net中的代码,在c#中进行翻译很容易)

Dim SortDirection As String, SortExpression As String
SortExpression = e.SortExpression
If (SortExpression = ViewState("SortExpression") And ViewState("SortDirection") = "asc") Then
    SortDirection = "desc"
Else
    SortDirection = "asc"
End If
ViewState("SortDirection") = SortDirection
ViewState("SortExpression") = SortExpression

Dim dt As Data.DataTable = ds.Tables(0) '<<<< fill ds with a method
dt.DefaultView.Sort = SortExpression & " " & SortDirection

GridView1.DataSource = dt.DefaultView
GridView1.DataBind()

就是这样。然后很容易将此代码放在泛型类中,以应用于其他网格视图

相关问题