GridView的排序问题 [英] GridView Sorting Question

查看:86
本文介绍了GridView的排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已有的GridView控件,我需要修复/在工作中提高在这里。基本上在GridView有标题和他们正在通过在code后面的一组数据绑定,它使用绑定列和模板列。问题是,我需要使每列sortible。什么是做到这一点,因为它不是标准的gridview的最好的办法?我需要做的标题链接,当点击在DESC或ASC进行排序。这里是我的GridView需要工作的一个例子。

I have an exisiting gridview that I need to fix/improve here at work. Basically the GridView has headings and they are being binded by a data set in the code behind, it uses BoundFields and TemplateFields. The problem is that I am needed to make each column sortible. What would be the best approach to do this since it is not the standard gridview?, I need to make the header links and when click to sort in DESC or ASC order. Here is an example of the gridview I need to work on.

<asp:GridView ID="theGrid" runat="server" CssClass="Grid" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" ForeColor="#FFFFFF" GridLines="None" OnPageIndexChanging="theGrid_PageIndexChanging" CaptionAlign="Left" OnRowCommand="theGrid_RowCommand" Width="100%" PageSize="25" AllowSorting="True" EmptyDataText="It's Empty.">
    <Columns>
        <asp:BoundField DataField="TestID" HeaderText="ID"/>
        <asp:TemplateField HeaderText="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">
            <ItemTemplate>
                <div align="right"><%# (DataBinder.Eval(Container.DataItem, "HouseNumber"))%></div>
            </ItemTemplate> 
        </asp:TemplateField>  

...等,什么将是绑定列和排序的最佳方法模板列?

...etc, What would be the best way to sort BoundFields and TemplateFields?

推荐答案

您需要设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sortex$p$pssion.aspx\"相对=nofollow> SORTEX pression 。

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

在codebehind可以存储当前SORTEX pression并在ViewState中的SortDirection:

in the codebehind you can store the current SortExpression and the SortDirection in ViewState:

Private Property SortExpression() As String
    Get
        If ViewState("SortExpression") Is Nothing OrElse ViewState("SortExpression").ToString().Length = 0  Then
            ViewState("SortExpression") = "Name ASC"
        End If
        Return ViewState("SortExpression").ToString
    End Get
    Set(ByVal value As String)
        ViewState("SortExpression") = value
    End Set
End Property

和GridView中的排序处理程序:

and in the Sorting-Handler of the GridView:

Protected Sub theGrid_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles theGrid.Sorting
    Dim currentSortColumn, currentSortDirection As String
    currentSortColumn = Me.SortExpression.Split(" "c)(0)
    currentSortDirection = Me.SortExpression.Split(" "c)(1)

    If e.SortExpression.Equals(currentSortColumn) Then
        'switch sort direction
        Select Case currentSortDirection.ToUpper
            Case "ASC"
                Me.SortExpression = currentSortColumn & " DESC"
            Case "DESC"
                Me.SortExpression = currentSortColumn & " ASC"
        End Select
    Else
        Me.SortExpression = e.SortExpression & " ASC"
    End If

    BindGrid() 'load the data with this SortExpression and DataBind the Grid'
End Sub

对不起,VB.NET,我发现为时已晚。你可以翻译在这里

Sorry for VB.NET, i've noticed too late. You could translate it here.

编辑:C#

private string SortExpression {
    get {
        if (ViewState("SortExpression") == null || ViewState("SortExpression").ToString().Length == 0) {
            ViewState("SortExpression") = "Name ASC";
        }
        return ViewState("SortExpression").ToString;
    }
    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();
}

这篇关于GridView的排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆