This is a migrated thread and some comments may be shown as answers.

Filter RadGrid with value from Dropdownlist

4 Answers 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 03 Jul 2010, 03:39 PM
Hello,

I have a Radgrid on my page which I want to filter with a value from a dropdownlist:

Protected Sub RadGrid_Suchresultate_NeedDataSource(ByVal source As ObjectByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid_Suchresultate.NeedDataSource  
        '=== Radgrid "Suchresultate" ==============================================================  
 
        Dim conn As New SqlConnection  
        conn.ConnectionString = ConfigurationManager.ConnectionStrings("BwaConnectionString").ConnectionString  
 
        Dim cmd_Suchresultate As New SqlCommand  
        cmd_Suchresultate.CommandType = CommandType.Text  
        cmd_Suchresultate.Connection = conn  
 
        Dim cmd_Kategorien As New SqlCommand  
        cmd_Kategorien.CommandType = CommandType.Text  
        cmd_Kategorien.Connection = conn  
 
        If Request.QueryString("Suche") <> "" Then 
 
            cmd_Suchresultate.CommandText = "SELECT [ArtikelID], [Artikelnummer], [Bezeichnung], [Preis], [Text], [Bild], [WarengruppeID], [Einheit] FROM [Artikel]" 
            cmd_Kategorien.CommandText = "SELECT DISTINCT W.Warengruppe, W.WarengruppeID FROM Artikel As A INNER JOIN Warengruppe As W ON A.WarengruppeID = W.WarengruppeID" 
 
            '--- Querystring "Suche" in String speichern ----------------------------------------------  
            Dim str_Suche As String = Request.QueryString("Suche")  
            '--- Suchbegriff(e) in Array speichern ----------------------------------------------------  
            Dim str_Suchbegriffe As String() = Split(str_Suche, " ")  
 
            Dim str_Operator As String = " WHERE " 
 
            For Each str_Suchbegriff As String In str_Suchbegriffe  
                cmd_Suchresultate.CommandText &= str_Operator & " " & "([Artikelnummer] LIKE '%' + @Artikelnummer" & str_Suchbegriff & " + '%' OR [Bezeichnung] LIKE '%' + @Bezeichnung" & str_Suchbegriff & " + '%' OR [Text] LIKE '%' + @Text" & str_Suchbegriff & " + '%')" 
                cmd_Kategorien.CommandText &= str_Operator & " " & "([Artikelnummer] LIKE '%' + @Artikelnummer" & str_Suchbegriff & " + '%' OR [Bezeichnung] LIKE '%' + @Bezeichnung" & str_Suchbegriff & " + '%' OR [Text] LIKE '%' + @Text" & str_Suchbegriff & " + '%')" 
                str_Operator = " AND " 
                '--- SqlParameter definieren --------------------------------------------------------------  
                'In der For - Each Schleife wird das Array "Suchbegriffe"  durchlaufen und die einzelnen Suchbegriffe der Variablen "Suchbegriff" zugewiesen  
                'An den Namen des Parameters wir der Suchbegriff angehängt, damit jeder Paramer einen anderen Namen erhält (sonst Fehler, Parameternamen müssen eindeutig sein)  
                With cmd_Suchresultate.Parameters  
                    .Add(New SqlParameter("@Artikelnummer" & str_Suchbegriff, str_Suchbegriff))  
                    .Add(New SqlParameter("@Bezeichnung" & str_Suchbegriff, str_Suchbegriff))  
                    .Add(New SqlParameter("@Text" & str_Suchbegriff, str_Suchbegriff))  
                End With 
                With cmd_Kategorien.Parameters  
                    .Add(New SqlParameter("@Artikelnummer" & str_Suchbegriff, str_Suchbegriff))  
                    .Add(New SqlParameter("@Bezeichnung" & str_Suchbegriff, str_Suchbegriff))  
                    .Add(New SqlParameter("@Text" & str_Suchbegriff, str_Suchbegriff))  
                End With 
                '--- SqlParameter definieren E N D E ------------------------------------------------------  
            Next 
 
            Page.Title = "Ihre Suchergebnisse für den Suchbegriff " & str_Suche 'TODO: Title besser machen  
 
        ElseIf Request.QueryString("Kategorie") <> "" Then 
            '--- Querystring "Kategorie" in String speichern ------------------------------------------  
            Dim str_Kategorie As String = Request.QueryString("Kategorie")  
            cmd_Suchresultate.CommandText = "SELECT [ArtikelID], [Artikelnummer], [Bezeichnung], [Preis], [Text], [Bild], [WarengruppeID], [Einheit] FROM [Artikel] WHERE WarengruppeID = @WarengruppeID" 
 
            '--- SqlParameter definieren --------------------------------------------------------------   
            With cmd_Suchresultate.Parameters  
                .Add(New SqlParameter("@WarengruppeID", str_Kategorie))  
            End With 
            '--- SqlParameter definieren E N D E ------------------------------------------------------  
 
            Page.Title = "Ihre gewünschte Kategorie " & str_Kategorie  
 
        Else 
            'Seite direkt aufgerufen  
            Response.Redirect("~/default.aspx")  
 
        End If 
 
        Dim dt_Suchresultate As New DataTable  
 
        Using conn  
            conn.Open()  
 
            Dim dr_Suchresultate As SqlDataReader = cmd_Suchresultate.ExecuteReader()  
            dt_Suchresultate.Load(dr_Suchresultate)  
            RadGrid_Suchresultate.DataSource = dt_Suchresultate  
 
            Dim dr_Kategorien As SqlDataReader = cmd_Kategorien.ExecuteReader()  
            drp_Kategorien.DataSource = dr_Kategorien  
            drp_Kategorien.DataValueField = "WarengruppeID" 
            drp_Kategorien.DataTextField = "Warengruppe" 
            drp_Kategorien.DataBind()  
        End Using  
 
        '=== Radgrid "Suchresultate" E N D E ======================================================  
 
    End Sub 

What is the best way to achieve that?

Thanks in adance for any help

Best regards

Tom

4 Answers, 1 is accepted

Sort by
0
Thomas
Top achievements
Rank 1
answered on 04 Jul 2010, 10:30 AM
I found a very good example in the Telerik help files, so I was able to sort this very easily.

Only one question left: How can I reset the filter? I thought maybe running Databind() again against the Grid, but that didn't do the trick. Any ideas?

Thanks

Tom
0
Mira
Telerik team
answered on 06 Jul 2010, 01:34 PM
Hello Thomas,

To reset all filters of a grid, you can clear the FilterExpression string and rebind the grid:
RadGrid1.MasterTableView.FilterExpression = "";
RadGrid1.Rebind();
You can see how it works in this demo.

Kind regards,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Thomas
Top achievements
Rank 1
answered on 06 Jul 2010, 01:43 PM
Hi Mira,

I tried that one before.
But if I do that, the Grid is empty, no records are showing up

Tom
0
Mira
Telerik team
answered on 07 Jul 2010, 09:07 AM
Hello Thomas,

Based on the supplied information, it is hard to determine what is causing this behavior.
Please open a formal support ticket and sent us a small working project, demonstrating your full setup and showing the issue.
We will debug it locally and get back to you.

Thank you in advance for the cooperation.

Regards,
Mira
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Thomas
Top achievements
Rank 1
Answers by
Thomas
Top achievements
Rank 1
Mira
Telerik team
Share this question
or