Filter A GridView After The Initial Bind

One of the goals that Microsoft has really pushed for in ASP.NET 2.0 is saving the amount of coding necessary to perform common tasks such as data access. On a recent project, I needed the ability to filter the results on a GridView control after I returned the results from my datasource. To accomplish this, I added a DropDownList and set the AutoPostBack property on the DropDownList to True. I added two values to the list; one that showed all of the results, and one that showed the filtered result set which in my case was a list of exceptions. I also added a SqlDataSource object called MySqlDataSource. I set the OnChange event to a subroutine similar to below:

Private Sub FilterDropDownList_Change(s as Object, e as EventArgs)
    If FilterDropDownList.SelectedValue = "Filter" then
        MySqlDataSource.FilterExpression = "MyColumn=1"
    Else
        MySqlDataSource.FilterExpression = ""
    End If

    MyGridView.DataBind
End Sub

I added the sub MyGridView.DataBind to the subroutine because this subroutine occurs after the SqlDataSource object is created and the resultset is filled. In reality, you only need to perform the MyGridView.DataBind when the FilterExpression value is set to something other than "".
Published Wednesday, May 31, 2006 10:12 AM by Jason N. Gaylord

Comments

# re: Filter A GridView After The Initial Bind

Friday, November 17, 2006 12:25 PM by pedro

Works perfectly - thanks.

# re: Filter A GridView After The Initial Bind

Tuesday, June 12, 2007 4:06 PM by Matt Cushing

I'd be curious to see if it works in my situation.  I have a stored procedure that brings back all the data I want.  I'd like to have several different gridviews based on a key in the results.  Gridview1 column = A.  Gridview2 column = B.

Would it be possible in the page_load to narrow down the results using what you have up there?

# re: Filter A GridView After The Initial Bind

Wednesday, June 20, 2007 5:07 PM by Robert

Gets stickier, when you try to filter with a "Like" where clause

# re: Filter A GridView After The Initial Bind

Wednesday, June 27, 2007 3:06 PM by Wade

Okay, here's one: How do you then edit a record after the filter is applied?

If I click on edit for a record, the gridview rebinds to datasource without the filter applied...which means that the record I would be editing isn't the one I want to edit.

# re: Filter A GridView After The Initial Bind

Tuesday, January 15, 2008 4:07 PM by Levi Johnson

The Like clause is easy. Instead of "MyColumn=1" you would just do "MyColumn LIKE '%text%'" and there you have it. I'm still trying to find how to find out how to keep the filter on postback, I suppose I'll just have to create the filter the the PreRender event.

# re: Filter A GridView After The Initial Bind

Tuesday, January 15, 2008 4:17 PM by Levi Johnson

instead of applying the filter in the SelectedIndexChanged event, put your filter in a viewstate. Then in PreRender of the gridview control, load the filter. Here's my example (my drpDownList is not autopostback, I click a button):

   Protected Sub btnSearch_Click()

'if user selects a field to search, search that field

'if user selects blank field, apply no filter

       If Not drpSearch.SelectedValue = "" Then

           ViewState("filter") = drpSearch.SelectedValue & " LIKE '%" & txtSearch.Text & "%'"

       Else

           ViewState("filter") = ""

       End If

       GridView1.DataBind()

   End Sub

   Protected Sub GridView1_PreRender()

'if viewstate is empty, no filter is applied

       If Not ViewState("filter") = "" Then SqlDataSource1.FilterExpression = ViewState("filter")

   End Sub

# re: Filter A GridView After The Initial Bind

Thursday, February 28, 2008 3:45 AM by Zankruti

Please give proper code of After intial Binding to GirdView.

# re: Filter A GridView After The Initial Bind

Monday, March 31, 2008 2:43 AM by Diane Montgomery

Thank you SO MUCH!  This is exactly what I needed and I can't believe how simple you made it.  It's 3am and my project is due TODAY.  I can now get a few more hours sleep!

# re: Filter A GridView After The Initial Bind

Wednesday, April 09, 2008 5:56 AM by Luke

I need this done, but without querying the database every time you want to filter. I've tried reading the DataSource and setting the filter on that, but the DataSource is null, even though I've already set and bound the control.

?

Leave a Comment

(required) 
(required) 
(optional)
(required)