Filter a gridview with a SQLDataSource using a checkbox

I have a gridview with a SQL Datasource. I am trying to filter my gridview with a checkbox. I want to use this checkbox to filter out my records. i.e. when it is not ticked I would like my gridview to show the results from the following query.

SELECT * FROM Products WHERE supplierID = 1

and when it is ticked I would like my gridview to show.

SELECT * FROM Products WHERE (supplierID = 1) AND (UnitPrice < 10 OR UnitPrice IS NULL)

I would prefer to do this with a paramaterised query but I am not sure how or whether it is even possible.

Thanks in advance for your help.

ohaitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nauman_ahmedCommented:
How many DataGrids are you using in the page?

-Nauman.
0
nauman_ahmedCommented:
To learn more about parameterized query please visit the following URL:

Using Parameterized Queries in ASP.Net
http://aspnet101.com/aspnet101/tutorials.aspx?id=1

-Nauman.
0
ohaitAuthor Commented:
Thanks.

There isn't a datagrid on the page just a gridview as I am using ASP.NET 2.0.  One other note.. The SQLDataSource has been added straight on to the design page not the code behind page.
0
ohaitAuthor Commented:
I found this code somewhere which when adapted seemed to solve my problem. sds is my SQlDataSource

    Protected Sub chkExcludeGrouped_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkExcludeGrouped.CheckedChanged
        If chkExcludeGrouped.Checked = True Then
            ViewState("sql") = "SELECT * FROM Products WHERE supplierID = 1"
        Else
            ViewState("sql") = "SELECT * FROM Products WHERE (supplierID = 1) AND (UnitPrice < 10 OR UnitPrice IS NULL)"
        End If
        sds.SelectCommand = ViewState("sql").ToString()
    End Sub


    Protected Overrides Sub OnLoad(ByVal e As EventArgs)

        MyBase.OnLoad(e)
        If Not ViewState("sql") Is Nothing Then
            sds.SelectCommand = ViewState("sql").ToString()
        End If

    End Sub
0
GranModCommented:
PAQed with points refunded (125)

GranMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.