对筛选的网格视图进行排序

本文关键字:排序 视图 网格 筛选 | 更新日期: 2023-09-27 18:36:04

问题:

我想允许在我的 GridView 上进行排序 (ASC/DESC) 和筛选。

我已经设法通过GridView上的DropDownListDataBound字段实现了两者。但是,当用户从 DropDownList 中选择筛选器,然后尝试对生成的数据进行排序时,GridView 会"忘记"当前选定的筛选器,而只是对我的所有数据进行排序,而不是像预期的那样对筛选的数据进行排序。

我尝试过:

这是我的过滤代码...

private void FilterGridView()
{
     SqlCommand command = new SqlCommand();
     SqlConnection connection = new SqlConnection();
     connection.ConnectionString = WebConfigurationManager.ConnectionStrings[1].ConnectionString;
     command.CommandText = string.Format("SELECT * FROM Products WHERE {0} = 1", ddlProdFilter.SelectedValue);
     if (ddlProdFilter.SelectedValue == "on_sale")
     {
            command.CommandText = "SELECT * FROM Products INNER JOIN ProductVariants ON [Products].product_id = [ProductVariants].product_id WHERE [ProductVariants].on_sale = 1";
     }
     else if (ddlProdFilter.SelectedValue == "*")
     {
            command.CommandText = "SELECT * FROM Products";
     }
     command.Connection = connection;
     SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
     DataSet ds = new DataSet();
     sqlAdapter.Fill(ds);
     if (ds.Tables[0].Rows.Count == 0)
     {
          pnlNoProducts.Visible = true;
     }
     else
     {
          pnlNoProducts.Visible = false;
     }
     gvAllProducts.DataSource = ds;
}

其中,下拉列表的选定值对应于表中的列名。

这是我的排序代码...

private void SortGridView(string sortExpression, string sortDir)
{
     SqlCommand command = new SqlCommand();
     SqlConnection connection = new SqlConnection();
     connection.ConnectionString = WebConfigurationManager.ConnectionStrings[1].ConnectionString;
     if (!string.IsNullOrEmpty(sortDir))
     {
         command.CommandText = "SELECT * FROM Products ORDER BY " + sortExpression + " " + sortDir;
     }
     command.Connection = connection;
     SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
     DataSet ds = new DataSet();
     sqlAdapter.Fill(ds);
     DataTable dt = new DataTable();
     dt = ds.Tables[0];
     gvAllProducts.DataSource = dt;
}

其中sortExpression对应于选定的DataBound字段,例如 product_id,最后,sortDir 是一个会话变量,用于准确跟踪回发之间的排序顺序。

事件处理程序。

protected void gvAllProducts_OnSorting(object sender, GridViewSortEventArgs e)
{
     if (e.SortDirection == SortDirection.Ascending && SessionHelper.GetSessionStringValue("SORT_DIRECTION") != "DESC")//if ascending and the last sort order wasn't descending, sort by DSC
     {
         SessionHelper.SetSessionValue("DESC", "SORT_DIRECTION");
     }
     else if (SessionHelper.GetSessionStringValue("SORT_DIRECTION") == "DESC")//otherwise, if the last sort order was desc, sort asc
     {
         SessionHelper.SetSessionValue("ASC", "SORT_DIRECTION");
     }
     SortGridView(e.SortExpression, SessionHelper.GetSessionStringValue("SORT_DIRECTION"));
     BindDataSource();
     e.Cancel = true;
}
protected void ddlProdFilter_SelectedIndexChanged(object sender, EventArgs e)
{
     FilterGridView();
     BindDataSource();
}
protected void gvAllProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
     gvAllProducts.PageIndex = e.NewPageIndex;
     FilterGridView();
     BindDataSource();
}

我尝试在进行排序时/之后调用过滤代码,这是有意义的,但同样的事情发生了。我已经在谷歌上搜索了大约 2 个小时,但找不到解决方案。

以前有人遇到过这个问题吗?您能在这里提供潜在的解决方案或一些指导吗?

谢谢

对筛选的网格视图进行排序

您在 SortGridView(..) 方法中的查询不考虑 ddl 值。您应该调用 FilterGridView(..) 方法 OnSort 事件或类似事件(例如,将其称为 BindGrid() )

设法通过在我的排序查询中包含 ddl 值来使其工作:

 private void SortGridView(string sortExpression, string sortDir, string filter)
        {
            SqlCommand command = new SqlCommand();
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = WebConfigurationManager.ConnectionStrings[1].ConnectionString;
            if (filter == "*")
            {
                command.CommandText = "SELECT * FROM Products ORDER BY " + sortExpression + " " + sortDir;
            }
            else if (filter == "on_sale")
            {
                command.CommandText = "SELECT * FROM Products INNER JOIN ProductVariants ON [Products].product_id = [ProductVariants].product_id WHERE [ProductVariants].on_sale = 1 ORDER BY [Products]." + sortExpression + " " + sortDir;
            }
            else
            {
                command.CommandText = "SELECT * FROM Products WHERE " + filter + " = 1 ORDER BY " + sortExpression + " " + sortDir;
            }
            command.Connection = connection;
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            sqlAdapter.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            gvAllProducts.DataSource = dt;
        }

与其使用用于FilterGridView的 void 方法,不如使用检索相应数据的"layer"方法。它应返回正确填充的数据集或集合对象,而不是直接分配数据源。这样,您的检索方法将设置任何必要的过滤和/或排序条件。然后在操作方法中,您将指定:

 gvAllProducts.DataSource = GetFilteredSortedData();

如果需要,可以构建它,以便它接受参数并对默认的"空"值使用重载。