The Problem:
I’d like to allow sorting (ASC/DESC) and filtering on my GridView.
I have managed to implement both via a DropDownList and DataBound fields on my GridView. However, when a user selects a filter from the DropDownList, then attempts to sort the resulting data, the GridView “forgets” the currently selected filter and just sorts all of my data, not the filtered data like you would expect.
What i’ve tried:
Here is my filtering code…
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;
}
Where the drop down lists selected value corresponds to a column name in my table.
Here is my sorting code…
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;
}
Where the sortExpression corresponds to a selected DataBound field e.g. product_id, and finally, sortDir is a session variable used accurately keep track of the sort order between postbacks.
Event handlers.
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();
}
I’ve tried calling the filtering code when/after i do my sorting which would make sense, but the same things occurs. I’ve been googling for about 2 hours and can’t find a solution to this.
Has anyone come up against this issue before? Could you offer a potential solution or some guidance here?
Thankyou
Managed to get it working by including the ddl value in my sort queries: