Imports Telerik.Web.UI
Imports System.Data.OleDb
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.IO

    Partial Class _Default
        Inherits System.Web.UI.Page
    Public Shared connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Nwind.mdb")

    Private Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        RadGrid1.DataSource = GetDataTable("SELECT Country, City FROM Customers")
    End Sub


    Private Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand
        If (e.CommandName = RadGrid.SortCommandName) Then
            Dim templateColumn As GridTemplateColumn = e.Item.OwnerTableView.GetColumnSafe("MyCustomTemplateColumn")
            templateColumn.SortExpression = e.CommandArgument
        End If
    End Sub

        Public Shared Function GetDataTable(ByVal query As String) As DataTable
            Dim connection1 As New OleDbConnection(connectionString)
            Dim adapter1 As New OleDbDataAdapter
            adapter1.SelectCommand = New OleDbCommand(query, connection1)
            Dim table1 As New DataTable
            connection1.Open()
            Try
                adapter1.Fill(table1)
            Finally
                connection1.Close()
            End Try
            Return table1
        End Function

    Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
        If (TypeOf e.Item Is GridFilteringItem) Then
            Dim filterItem As GridFilteringItem = CType(e.Item, GridFilteringItem)
            filterItem("MyCustomTemplateColumn").Controls.Clear()

            Dim ddList As DropDownList = New DropDownList()
            ddList.AutoPostBack = True
            ddList.ID = "ddList1"
            ddList.Width = Unit.Percentage(50)

            AddHandler ddList.SelectedIndexChanged, AddressOf ddList1_SelectedIndexChanged

            filterItem("MyCustomTemplateColumn").Controls.Add(ddList)

            ddList = New DropDownList()
            ddList.AutoPostBack = True
            ddList.ID = "ddList2"
            ddList.Width = Unit.Percentage(50)

            AddHandler ddList.SelectedIndexChanged, AddressOf ddList2_SelectedIndexChanged

            filterItem("MyCustomTemplateColumn").Controls.Add(ddList)
        End If
    End Sub
    Private Sub ddList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ddList As DropDownList = CType(sender, DropDownList)
        Session("ddlSelValue1") = ddList.SelectedValue

        RadGrid1.MasterTableView.FilterExpression = "([Country] = '" & ddList.SelectedValue.ToString() & "')"
        RadGrid1.MasterTableView.Rebind()
    End Sub
    Private Sub ddList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ddList As DropDownList = CType(sender, DropDownList)
        Session("ddlSelValue2") = ddList.SelectedValue

        RadGrid1.MasterTableView.FilterExpression = "([City] = '" & ddList.SelectedValue.ToString() & "')"
        RadGrid1.MasterTableView.Rebind()
    End Sub

    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If (TypeOf e.Item Is GridFilteringItem) Then
            Dim filterItem As GridFilteringItem = CType(e.Item, GridFilteringItem)

            Dim ddList As DropDownList = filterItem.FindControl("ddList1")
            ddList.DataTextField = "Country"
            ddList.DataValueField = "Country"
            ddList.DataSource = GetDataTable("SELECT DISTINCT Country FROM Customers")

            ddList.DataBind()

            ddList.Items.Insert(0, New ListItem("Choose option to filter"))

            If (Not Session("ddlSelValue1") Is Nothing) Then
                ddList.Items.FindByValue(CType(Session("ddlSelValue1"), String)).Selected = True
            Else
                ddList.SelectedIndex = 0
            End If

            ddList = filterItem.FindControl("ddList2")
            ddList.DataTextField = "City"
            ddList.DataValueField = "City"
            ddList.DataSource = GetDataTable("SELECT DISTINCT City FROM Customers")
            ddList.DataBind()

            ddList.Items.Insert(0, New ListItem("Choose option to filter"))

            If (Not Session("ddlSelValue2") Is Nothing) Then
                ddList.Items.FindByText(CType(Session("ddlSelValue2"), String)).Selected = True
            Else
                ddList.SelectedIndex = 0
            End If
        End If
    End Sub

    Protected Sub btnClearFilters_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClearFilters.Click
        RadGrid1.MasterTableView.FilterExpression = String.Empty
        Session("ddlSelValue1") = Nothing
        Session("ddlSelValue2") = Nothing

        RadGrid1.MasterTableView.Rebind()
    End Sub
End Class


