Imports Telerik.Web.UI
Imports System.Data.OleDb
Imports System.Data
Partial Class WebUserControl
    Inherits System.Web.UI.UserControl

    Dim ds As DataSet
    Dim dt As DataTable

    Public Shared ReadOnly Property connectionString() As String
        Get
            Dim dbPath As String = System.Web.HttpContext.Current.Server.MapPath("~\App_Data\Nwind.mdb")
            Return ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbPath)
        End Get
    End Property

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

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            ds = GetDataSet("SELECT Country, City, Index FROM Customers")
            Me.RadGrid1.MasterTableView.Columns.Clear()
            For Each dataColumn As DataColumn In ds.Tables(0).Columns
                Dim gridColumn As RadGrid.MyCustomFilteringColumn = New RadGrid.MyCustomFilteringColumn
                Me.RadGrid1.MasterTableView.Columns.Add(gridColumn)
                gridColumn.DataField = dataColumn.ColumnName
                gridColumn.HeaderText = dataColumn.ColumnName
            Next
        End If
    End Sub

    Private Sub RadGrid1_ColumnCreating(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridColumnCreatingEventArgs) Handles RadGrid1.ColumnCreating
        If (e.ColumnType = GetType(RadGrid.MyCustomFilteringColumn).Name) Then
            e.Column = New RadGrid.MyCustomFilteringColumn
        End If
    End Sub

    Private Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
        If (e.CommandName = "Filter") Then
            For Each column As GridColumn In e.Item.OwnerTableView.Columns
                column.CurrentFilterValue = String.Empty
                column.CurrentFilterFunction = GridKnownFunction.NoFilter
            Next
        End If
    End Sub

    Private Sub clrFilters_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles clrFilters.Click
        For Each column As GridColumn In RadGrid1.MasterTableView.Columns
            column.CurrentFilterFunction = GridKnownFunction.NoFilter
            column.CurrentFilterValue = String.Empty
        Next
        RadGrid1.MasterTableView.FilterExpression = String.Empty
        RadGrid1.MasterTableView.Rebind()
    End Sub

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

    Public Shared Function GetDataSet(ByVal query As String) As DataSet
        Dim MyOleDbConnection As OleDbConnection = New OleDbConnection(connectionString)
        Dim MyOleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter
        MyOleDbDataAdapter.SelectCommand = New OleDbCommand(query, MyOleDbConnection)
        Dim myDataSet As DataSet = New DataSet("Customers")
        MyOleDbConnection.Open()
        Try
            MyOleDbDataAdapter.Fill(myDataSet)
        Finally
            MyOleDbConnection.Close()
        End Try
        Return myDataSet
    End Function

End Class
