在ASP.NET中对GridView列进行排序

6
我有一个从数据库加载数据的网格视图。数据在第一次排序时按日期时间排序显示在网格视图中。我想在该网格视图的一个列上允许排序,例如在main_post列上。我尝试过了这个方法,但是当我点击该列的标题时,网格视图仍然没有进行排序。
这是我的网格视图前端代码:
<asp:DataGrid ID="Datagrid1" runat="server" 
    AllowPaging="True" 
    AllowSorting="True" 
    OnSorting="ComponentGridView_Sorting">
    <Columns>
        <asp:BoundColumn DataField="main_post" HeaderText="Header Post ID" 
            SortExpression="ComponentGridView_Sorting" />
    </Columns>
</asp:DataGrid>

我的后端代码:

protected void loadData()
{
    DataTable dtTemp;
    dtTemp = objDBInterface.getResults(strSQL);
    Datagrid1.DataSource = dtTemp;
    Datagrid1.DataBind();
    ViewState["dtbl"] = dtTemp;
}

protected void ComponentGridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = ViewState["dtbl"] as DataTable;
        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
            Datagrid1.DataSource = dataView;
            Datagrid1.DataBind();
        }
    }
    private string ConvertSortDirection(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;
        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;
            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

我没有任何错误,也不知道我的错误在哪里,请有人帮我解决这个问题吗?
提前致谢。
4个回答

5

简单来说,只需要将Allowsorting属性设置为true,并在你的gridview中添加排序事件即可。

<asp:GridView ID="GridView1" runat="server" AllowSorting="true" 
      OnSorting="GridView1_Sorting">
</asp:GridView>   

现在在 .cs 文件中添加 OnSorting 事件。

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{ 
    SetSortDirection(SortDireaction);
    if (dataTable != null)
    {
       dataTable.DefaultView.Sort = e.SortExpression + " " +_sortDirection;
        GridView1.DataSource = dataTable;
        GridView1.DataBind();
        SortDireaction = _sortDirection;
    }
} 
protected void SetSortDirection(string sortDirection)
{
    if (sortDirection == "ASC")
    {
        _sortDirection = "DESC";
    }
    else
    {
        _sortDirection = "ASC";
    }
} 


2
我找到了解决方案。希望这对其他遇到类似问题的人有所帮助。在 .aspx 页面中需要更改以下内容: 从 OnSorting="ComponentGridView_Sorting" 改为 onsortcommand="ComponentGridView_Sorting" 然后将此代码放置在 aspx.cs(后端代码)中:
protected void Datagrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
    {
        string strSQL;
        DataTable dt;
        strSQL = "YOUR SELECT STATEMENT (SQL)";
        dt = strSQL;
            {
                string SortDir = string.Empty;
                if (dir == SortDirection.Ascending)
                {
                    dir = SortDirection.Descending;
                    SortDir = "Desc";
                }
                else
                {
                    dir = SortDirection.Ascending;
                    SortDir = "Asc";
                }
                DataView sortedView = new DataView(dt);
                sortedView.Sort = e.SortExpression + " " + SortDir;
                Datagrid1.DataSource = sortedView;
                Datagrid1.DataBind();
            }
    }

    protected SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }

1
不要紧张,开发者们,按照我的步骤非常容易。 第一步:创建GridView。
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"  class="w3-table-all"
        OnPageIndexChanging="OnPageIndexChanging" PageSize="20" >
        <Columns>
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="250px" DataField="sn_no" HeaderText="ID" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="name" HeaderText="Name" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="mo" HeaderText="Mobile" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="email" HeaderText="Email Id" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="loc" HeaderText="Location" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="date" HeaderText="Date" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="day2" HeaderText="Day" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="date" HeaderText="Date" />
            <asp:BoundField HeaderStyle-CssClass="w3-blue" ItemStyle-CssClass="w3-hover-green" ItemStyle-Width="500px" DataField="time" HeaderText="Time" />
        </Columns>

  </asp:GridView>

步骤2 前往*.aspx.cs文件并编写

a). 用于降序排列

cmd = new MySqlCommand("SELECT * FROM appointment ORDER BY sn_no DESC", con);
英译中:

b). 对于升序排列

cmd = new MySqlCommand("SELECT * FROM appointment ORDER BY sn_no ASC", con);

感谢 #Vaibhav Yadav Vaibhav Designs http://vaibhavdesigns.org 的支持,更多信息请访问...


评论大家 - iRIS

0
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="empid" DataSourceID="SqlDataSource1" EnableModelValidation="True" ForeColor="#333333" GridLines="None">

4
你可以对你的回答提供一些解释。 - guisantogui

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接