Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:14:56+00:00 2026-06-04T22:14:56+00:00

The Problem: I’d like to allow sorting (ASC/DESC) and filtering on my GridView. I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:14:58+00:00Added an answer on June 4, 2026 at 10:14 pm

    Managed to get it working by including the ddl value in my sort queries:

     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;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have a WCF service that invokes methods via reflection and sends the
Problem is like this: I have two winapi application's. There is only one way
Problem: I have a table that prints out vertical but I would like it
Problem description I have 6 databases from 6 different machines, and having one cloud
Problem Statement : Execute Various Command randomly by matching its percentage. like execute CommandA
Problem: I would like to download 100 files in parallel from AWS S3 using
Problem: I have a list containg hours, for example: 08:15:00 08:45:00 09:00:00 12:00:00 ...
Problem Description: We have a service which has applications for main mobile OS’s. We
Problem! I Have the following input (rules) from a flat file (talking about numeric
Problem PHP session in different folder. I have problem with PHP session. There are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.