Click here to Skip to main content
15,902,635 members
Articles / .NET
Tip/Trick

How to allow the user to filter out a gridview by multiple selection

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
27 Jan 2010CPOL 7.9K   1  
My contribution to my fellow developers & to make our life easier. The following code allow the user to filter out a gridview depending on their Selection. 'Here is the Complete Code Behind that handles the GridView and 4 'Different DropDownlist each one representing a Filter....
My contribution to my fellow developers & to make our life easier. The following code allow the user to filter out a gridview depending on their Selection.


VB
'Here is the Complete Code Behind that handles the GridView and 4 
'Different DropDownlist each one representing a Filter. 
Imports System.IO
Imports System.Data
Imports System.Net.Mail
Imports System.Web
Imports System.Globalization
Imports System
Imports system.Data.SqlClient
Partial Class pmo_projectlist
    Inherits System.Web.UI.Page
    Protected WithEvents daTableData As System.Data.SqlClient.SqlDataAdapter
    Protected WithEvents dsTableData As System.Data.DataSet
    Protected WithEvents dvTableData As System.Data.DataView
    Dim cnProj As New SqlConnection(
        System.Configuration.ConfigurationManager.ConnectionStrings("ProjectsConnectionString").ConnectionString)
    Dim cmdSP As SqlClient.SqlCommand
    Dim prmSP As SqlClient.SqlParameter
    Dim Executivex As DropDownList
    

    Private Sub Page_Load(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load

        If Not Page.IsPostBack Then
         
        End If

        All()
    End Sub


    Protected Sub GridView1_rowdatabound(ByVal sender As Object,
        ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        Dim projidx
        projidx = e.Row.Cells(0).Text
        If e.Row.RowIndex <> -1 Then
            e.Row.Cells(9).Text = "<a href=project.aspx?projid=" & projidx & ">Edit</a>"

        End If

    End Sub

    Sub Filter()

        Dim Executivex = ddl_executive_sponsor.SelectedItem.Text
        Session("strExecutive") = Executivex
        Dim Leadx = ddl_project_lead.SelectedValue
        Session("strLead") = Leadx
        Dim FAx = ddl_functional_area.SelectedItem.Text
        Session("strFA") = FAx
        Dim Statusx = ddl_projStatus.SelectedItem.Text
        Session("strStatus") = Statusx
        Exit Sub
      
    End Sub
    Private Sub All()
        Try
            '_-----------------Full View------------------
            If (cnProj.State = Data.ConnectionState.Closed) Then
                cnProj.Open()
            End If

            cmdSP = New Data.SqlClient.SqlCommand("PMO_sp_filter3", cnProj)
            cmdSP.CommandType = Data.CommandType.StoredProcedure
            cmdSP.CommandTimeout = "200"
            prmSP = cmdSP.Parameters.Add("@SQLType", Data.SqlDbType.VarChar, 250)
            prmSP.Value = "Full_Filter"


            daTableData = New System.Data.SqlClient.SqlDataAdapter(cmdSP)
            dsTableData = New DataSet
            daTableData.FillSchema(dsTableData, SchemaType.Source, "TableData")
            daTableData.Fill(dsTableData, "TableData")

            cmdSP.ExecuteNonQuery()
            cnProj.Close()


            dvTableData = New DataView
            dvTableData.Table = dsTableData.Tables(0)
            dvTableData.Sort = Session("strODSSortField") & Session("strODSSortOrder")
            GridView1.DataSource = dvTableData
            GridView1.DataMember = "Projects"
            GridView1.DataBind()


        Catch xcp As Exception
            lblErrorMessage.Visible = True
            lblErrorMessage.Text = "Error on Loading Project Details Drop Downs List. " & xcp.Message
        End Try

    End Sub

    Private Sub Filters()

        '_-----------------Executive------------------
        Filter()
        If ddl_executive_sponsor.SelectedItem.Text = "Select" Then
            Session("strExecutive") = DBNull.Value
        End If
        'If ddl_project_lead.SelectedValue = "0" Then
        '    Session("strLead") = ""
        'End If
        If ddl_functional_area.SelectedItem.Text = "Select" Then
            Session("strFA") = DBNull.Value
        End If
        If ddl_projStatus.SelectedItem.Text = "Select" Then
            Session("strStatus") = DBNull.Value
        End If

        If (cnProj.State = Data.ConnectionState.Closed) Then
            cnProj.Open()
        End If

        cmdSP = New Data.SqlClient.SqlCommand("PMO_sp_filter3", cnProj)
        cmdSP.CommandType = Data.CommandType.StoredProcedure
        cmdSP.CommandTimeout = "200"
        prmSP = cmdSP.Parameters.Add("@SQLType", Data.SqlDbType.VarChar, 250)
        prmSP.Value = "Filters"
        prmSP = cmdSP.Parameters.Add("@Executive", Data.SqlDbType.VarChar, 250)
        prmSP.Value = Session("strExecutive")
        prmSP = cmdSP.Parameters.Add("@Leader", Data.SqlDbType.VarChar, 250)
        prmSP.Value = Session("strLead")
        prmSP = cmdSP.Parameters.Add("@FA", Data.SqlDbType.VarChar, 250)
        prmSP.Value = Session("strFA")
        prmSP = cmdSP.Parameters.Add("@Status", Data.SqlDbType.VarChar, 250)
        prmSP.Value = Session("strStatus")


        daTableData = New System.Data.SqlClient.SqlDataAdapter(cmdSP)
        dsTableData = New DataSet
        daTableData.FillSchema(dsTableData, SchemaType.Source, "TableData")
        daTableData.Fill(dsTableData, "TableData")

        cmdSP.ExecuteNonQuery()
        cnProj.Close()


        dvTableData = New DataView
        dvTableData.Table = dsTableData.Tables(0)
        dvTableData.Sort = Session("strODSSortField") & Session("strODSSortOrder")
        GridView1.DataSource = dvTableData
        GridView1.DataMember = "Projects"
        GridView1.DataBind()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        If ddl_executive_sponsor.SelectedItem.Text <> "Select" Or ddl_project_lead.SelectedItem.Text <> "Select" _
        Or ddl_functional_area.SelectedItem.Text <> "Select" Or ddl_projStatus.SelectedItem.Text <> "Select" Then
            Filters()
        Else
            All()
        End If

    End Sub
    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, 
    ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

        If ddl_executive_sponsor.SelectedItem.Text <> "Select" Then
            Filters()
            Me.GridView1.PageIndex = e.NewPageIndex
            Me.GridView1.DataBind()
        ElseIf ddl_project_lead.SelectedItem.Text <> "Select" Then
            Filters()
            Me.GridView1.PageIndex = e.NewPageIndex
            Me.GridView1.DataBind()
        ElseIf ddl_functional_area.SelectedItem.Text <> "Select" Then
            Filters()
            Me.GridView1.PageIndex = e.NewPageIndex
            Me.GridView1.DataBind()
        ElseIf ddl_projStatus.SelectedItem.Text <> "Select" Then
            Filters()
            Me.GridView1.PageIndex = e.NewPageIndex
            Me.GridView1.DataBind()
        Else
            All()
            Me.GridView1.PageIndex = e.NewPageIndex
            Me.GridView1.DataBind()
        End If

    End Sub

End Class

To Complete the Filter Out for the GridView you must call the Stored Procedure located in your database. I am including the Stored Procedure Syntax to make your life easy.

SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE  [dbo].[Use your own name ] 
(
@SQLType          	nvarchar(100) = NULL,
@Executive nvarchar(250) = Null,
@Leader int = Null,
@FA nvarchar (250) = Null,
@Status nvarchar (250) = Null
)	 
AS
/********************************************************************
*	Program Name	: 	 
*
*	Create Date	:   
*
*	Author		: 	Osirisa
*
*	Purpose		:	Create a Gridview Filter for the Main Page
*********************************************************************
*
*	Modification History: 
*
*********************************************************************

--*************************************************************
--SET SQL STMT BASED ON SQL TYPE PASSED IN 
--*************************************************************/
Declare @Leader2 int
Set @Leader2 = @Leader

If @SQLType = 'Filters'
begin

-- Handle Empty Paramaters. 
If
@Executive IS NULL 
SET @Executive = '%' 

If
@Leader IS NULL 
SET @Leader = '0'
If @Leader = '0' SET @Leader2 ='1000'
 
IF
@FA IS NULL 
SET @FA = '%'
 

IF
@Status IS NULL 
SET @Status = '%'

 
-- Insert statements for procedure here 

SELECT PMO_Projects.project_number, PMO_Projects.project_name, 
PMO_Executives.executive_full, 
PMO_Employees.[Last Name] + N',  ' + PMO_Employees.[First Name] 
AS project_lead, PMO_Functional_Area.functional_area, 
    PMO_Projects.completion, PMO_Projects.project_start_date, 
    PMO_Projects.project_end_date, PMO_Project_Status.code FROM 
PMO_Projects LEFT OUTER JOIN PMO_Functional_Area ON 
PMO_Projects.functional_area = PMO_Functional_Area.id
LEFT OUTER JOIN PMO_Employees ON 
PMO_Projects.project_lead = PMO_Employees.ID 
LEFT OUTER JOIN PMO_Executives ON 
PMO_Projects.executive_sponsor = PMO_Executives.ID
LEFT OUTER JOIN PMO_Project_Status   on
PMO_PROJECT_Status.ID = PMO_Projects.Project_Status
WHERE
 ((Executive_full LIKE @Executive) and (
   Project_Lead BETWEEN @Leader and @Leader2 ) and (
   PMO_Functional_Area.functional_area Like @FA) and (PMO_Project_Status.code  Like @Status))
ORDER BY PMO_Projects.project_number
END

If @SQLType = 'Full_Filter'
Begin
SELECT PMO_Projects.project_number, PMO_Projects.project_name, 
PMO_Executives.executive_full, 
PMO_Employees.[Last Name] + N',  ' + PMO_Employees.[First Name] 
AS project_lead, PMO_Functional_Area.functional_area,
    PMO_Projects.completion, PMO_Projects.project_start_date, 
    PMO_Projects.project_end_date, PMO_Project_Status.code FROM 
PMO_Projects LEFT OUTER JOIN PMO_Functional_Area ON 
PMO_Projects.functional_area = PMO_Functional_Area.id
 LEFT OUTER JOIN PMO_Employees ON 
PMO_Projects.project_lead = PMO_Employees.ID 
LEFT OUTER JOIN PMO_Executives ON 
PMO_Projects.executive_sponsor = PMO_Executives.ID
LEFT OUTER JOIN PMO_Project_Status   on
PMO_PROJECT_Status.ID = PMO_Projects.Project_Status
ORDER BY PMO_Projects.project_number
End

SELECT @LEADER
SELECT @LEADER2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --