【问题标题】:Sort a GridView without going back to SQL在不返回 SQL 的情况下对 GridView 进行排序
【发布时间】:2015-04-16 20:13:00
【问题描述】:

我有一个gridview,我用它来显示来自storedProcedure 的数据。我现在想对数据进行排序,但很多地方都说我必须回到 sql 才能做到这一点。可能我不理解给出的解释,但我认为这是不对的。我已经简化了问题,并希望获得有关如何对 boundcolumn 进行排序的任何帮助。这是我的 aspx 页面。

    <asp:GridView  ID="BannerGrid" runat="server" AllowSorting="True" onSorting="Sorts" GridLines="None" AutoGenerateColumns="false" OnRowCreated="BannerGrid_RowCreated">
        <Columns>
            <asp:Boundfield DataField="BannerID" HeaderText="Banner ID" SortExpression="BannerID"/> </Columns>

后面是我的 c# 代码:

            SqlConnection sqlConnection1 = new SqlConnection(conn);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "GetDifferenceInteraction";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ADate", TodayDate);
        cmd.Parameters.AddWithValue("@BDate", YesDate);
        SqlDataReader reader;
        cmd.Connection = sqlConnection1;

        sqlConnection1.Open();

        reader = cmd.ExecuteReader();
        BannerGrid.DataSource = reader;
        BannerGrid.DataBind();
        reader.Close();
        sqlConnection1.Close();

关于如何在 c# itelf 中排序的任何帮助。提前致谢!

【问题讨论】:

标签: c# asp.net sorting gridview


【解决方案1】:

您可以将数据存储在 ViewState 变量中,但如果您的数据非常大,可能会导致问题。

...
reader = cmd.ExecuteReader();
//here you should save your data, I stored mine in a DataTable type like this
dtStored.Load(reader);
BannerGrid.DataSource = reader;
BannerGrid.DataBind();
reader.Close();
...

我有这四个属性

    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    private SortDirection gvSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                ViewState["sortDirection"] = SortDirection.Ascending;

            return (SortDirection)ViewState["sortDirection"];
        }
        set { ViewState["sortDirection"] = value; }
    }
    private DataTable dtStored
    {
        get { return (ViewState["dt"] == null) ? null : (DataTable)ViewState["dt"]; }
        set { ViewState["dt"] = value; }
    }

在您的排序活动中,您可以拥有这个

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

        if (gvSortDirection == SortDirection.Ascending)
        {
            gvSortDirection = SortDirection.Descending;
            direction = DESCENDING;
        }
        else
        {
            gvSortDirection = SortDirection.Ascending;
            direction = ASCENDING;
        }
        try
        {   
            DataTable dt = dtStored;

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

            BannerGrid.DataSource = dv;
            BannerGrid.DataBind();     
        }
        catch (Exception ex)
        {
            //Log error
        }
    }

【讨论】:

  • 谢谢,这正是我要找的!
【解决方案2】:

如果我没记错的话,您可以将数据加载到其中包含 DataTable 的 DataSet。然后将其绑定到您的 GridView。这提供了很多 更多功能可以让您进行排序。但最好安排一个首先生成结果集的查询。

【讨论】:

    猜你喜欢
    • 2017-05-07
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2017-10-14
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多