Filter data and update GridView

Last post 05-12-2008 2:01 PM by Lotos1. 2 replies.

Sort Posts:

  • Filter data and update GridView

    05-11-2008, 9:20 PM
    • Loading...
    • Lotos1
    • Joined on 04-10-2008, 5:11 PM
    • Posts 15

    I have a GridView that gets data from an SqlDataSource. It works fine, but now when I want to filter the results and add more parameters to my sql query, nothing happens. My initial select statement in the SqlDataSource contains only 2 parameters, so now when a user clicks a Button, onclick I call a function that is supposed to change the select statement of the SqlDataSource, add 2 more parameters taken from 2 controls, and then update the GridView. This is what I use: dataSource.SelectCommand = "new select command here";  dataSource.SelectParameters.Add("State", txtState.Text); dataSource.SelectParameters.Add("City", txtCity.Text);  grid.DataBind(); It compiles, but when I click the button to filter the results, the GridView always looks the same as if nothing happened. I do not know if this is the right way to do it, or am I on the wrong track? Help is appreciated. Thank you in advance.

  • Re: Filter data and update GridView

    05-12-2008, 8:12 AM
    Answer
    • Loading...
    • SrDhUS
    • Joined on 12-28-2004, 10:17 AM
    • IL,Chicago
    • Posts 94

    You can use  FilterExpression as shown below. You can also add parameters by using the GridView.FilterParameters collection.

    SqlDataSource1.FilterExpression = "CategoryName like 'C%'";

    Without hard cording you can dynamically create the where conditon as below: 

    SqlDataSource1.FilterExpression = "Status = {0}";
    SqlDataSource1.FilterParameters.Add(p); // where p is a Parameter

    The {0} in the filter expression represents the first item in the FilterParameters collection. {1}, {2} etc would work for the second and third items etc.
     

    Please remember to click “Mark as Answer” on the post that helps you
  • Re: Filter data and update GridView

    05-12-2008, 2:01 PM
    • Loading...
    • Lotos1
    • Joined on 04-10-2008, 5:11 PM
    • Posts 15

    It works, thank you. But you were missing single quotes around '{0}'. Without the quotes, I was getting that the column was not found because it would not look for the value, but the actual column name that matches {0}.

    SqlDataSource1.FilterExpression = "Status='{0}'";
    SqlDataSource1.FilterParameters.Add(p); // where p is a Parameter

Page 1 of 1 (3 items)