问题:
我想在GridView上允许排序(ASC / DESC)和过滤。
我已设法通过DropDownList
上的DataBound
和GridView
字段实现这两个字段。但是,当用户从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个小时,无法找到解决方案。
以前有人遇到过这个问题吗?你能提供一个潜在的解决方案或一些指导吗?
三江源
答案 0 :(得分:2)
通过在我的排序查询中包含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;
}
答案 1 :(得分:1)
您在SortGridView(..)方法中的查询未考虑ddl值。你应该调用你的FilterGridView(..)方法OnSorting事件,或类似的(例如,称之为BindGrid())
答案 2 :(得分:1)
而不是FilterGridView
的void方法,你应该有一个“层”方法来检索适当的数据。它应返回正确填充的DataSet或Collection对象,而不是直接分配数据源。这样,您的检索方法将设置必要的任何过滤和/或排序条件。然后在您的操作方法中指定:
gvAllProducts.DataSource = GetFilteredSortedData();
如果需要,可以构建它以便它接受参数并使用重载作为默认的“空”值。