?
Solved

Filter a gridview with a SQLDataSource using a checkbox

Posted on 2006-03-22
6
Medium Priority
?
1,586 Views
Last Modified: 2012-06-27
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.

0
Comment
Question by:ohait
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 16263376
How many DataGrids are you using in the page?

-Nauman.
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 16263393
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
 

Author Comment

by:ohait
ID: 16264182
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
 

Author Comment

by:ohait
ID: 16280323
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
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16571613
PAQed with points refunded (125)

GranMod
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question