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

asp.net gridview排序自定义数据源,asp.net,gridview,Asp.net,Gridview,我有一个正在填充的gridview ok。现在我想启用排序。我完成了所有必需的代码——即启用标记排序,并在用户排序时提供要调用的事件 这是我错过的排序事件——我已经尝试过谷歌的一些实现,但我不太确定。本质上,我是不是说我需要根据用户想要排序的列以及ASC或DESC向服务器提供新的查询?如果是这样的话,听起来还有很多工作要做……更多的查询 谢谢 达莫 隐藏代码以绑定网格 // Load the main homepage data to the grid

我有一个正在填充的gridview ok。现在我想启用排序。我完成了所有必需的代码——即启用标记排序,并在用户排序时提供要调用的事件

这是我错过的排序事件——我已经尝试过谷歌的一些实现,但我不太确定。本质上,我是不是说我需要根据用户想要排序的列以及ASC或DESC向服务器提供新的查询?如果是这样的话,听起来还有很多工作要做……更多的查询

谢谢 达莫

隐藏代码以绑定网格

 // 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>

我说我需要向客户提供新的查询是否正确 服务器,具体取决于用户希望按哪个列排序以及ASC或 还有什么?如果是这样的话,听起来还有很多工作要做……更多的查询

是的,你说得对。您需要再次查询数据源以应用新排序。但最后一句是不正确的。您只需要更改sql的
顺序(或用于排序
数据源的任何内容)。您可以对订单列和方向使用一个
ViewState
变量(您需要在回发过程中保持它,因此是ViewState)。我将向您展示一个示例:

首先,您需要设置

下面是
排序
事件处理程序。请注意,
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();
}

我有一个更简单的方法,只需几行代码:(vb.net中的代码,用于c语言的翻译,很容易)

Dim SortDirection为字符串,SortExpression为字符串
SortExpression=e.SortExpression
如果(SortExpression=ViewState(“SortExpression”)和ViewState(“SortDirection”)=asc),则
SortDirection=“desc”
其他的
SortDirection=“asc”
如果结束
视图状态(“排序方向”)=排序方向
ViewState(“SortExpression”)=SortExpression

Dim dt As Data.DataTable=ds.Tables(0)'查询需要处理排序方向是正确的。您还需要决定在视图状态、url或会话中存储当前排序方向的位置。您好,在代码末尾,您可以说“//使用此SortExpression加载数据并将数据绑定到网格”。您的意思是“//使用此SortExpression和currentSortColumn加载数据并将数据绑定到网格”@user1438082:否,正如您所看到的,
SortExpression
包含这两个选项,
currentSortColumn
+
SortDirection
。这意味着您只需将SortExpression附加到sql的
ORDER BY
的末尾。这种方法支持双向排序,如果用户连续单击同一列,
SortDirection
将更改该列的排序方向。请注意,属性
SorteExpression
有一个默认排序,请相应地更改它。我发现,到目前为止,使用telerik或devexpress gridview控件更容易。它们具有内置功能,可为您节省数周的时间
private string SortExpression {
    get {
        if(String.IsNullOrWhiteSpace((String)ViewState["SortExpression"])
            ViewState["SortExpression"] = "Name ASC";

        return (String)ViewState["SortExpression"];
    }
    set { ViewState["SortExpression"] = value; }
}
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();
}
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()