Imports Telerik.WebControls
Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page

    Private Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        If (Session("gridSource") Is Nothing) Then
            Dim myDataTable As DataTable = BuildDataSourceClass.GetDataTable("SELECT TOP 40 EmployeeID, FirstName, LastName, Title FROM Employees")
            RadGrid1.DataSource = myDataTable.DefaultView

            myDataTable.PrimaryKey = New DataColumn() {myDataTable.Columns("EmployeeID")}

            Session("gridSource") = myDataTable
        Else
            RadGrid1.DataSource = CType(Session("gridSource"), DataTable)
        End If



    End Sub

    Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemCreated
        If (TypeOf e.Item Is GridDataItem) Then
            Dim editAnchor As HtmlAnchor = CType(e.Item.FindControl("EditAnchor"), HtmlAnchor)
            editAnchor.Attributes("href") = ""
            editAnchor.Attributes("onclick") = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("EmployeeID"), e.Item.ItemIndex)
        End If

    End Sub
    Protected Overrides Sub RaisePostBackEvent(ByVal source As IPostBackEventHandler, ByVal eventArgument As String)
        MyBase.RaisePostBackEvent(source, eventArgument)
        If TypeOf source Is RadGrid Then
            Select Case eventArgument
                Case "Rebind"
                    RadGrid1.MasterTableView.SortExpressions.Clear()
                    RadGrid1.MasterTableView.GroupByExpressions.Clear()
                    RadGrid1.Rebind()
                Case "RebindAndNavigate"
                    RadGrid1.MasterTableView.SortExpressions.Clear()
                    RadGrid1.MasterTableView.GroupByExpressions.Clear()
                    RadGrid1.MasterTableView.CurrentPageIndex = RadGrid1.MasterTableView.PageCount - 1
                    RadGrid1.Rebind()
            End Select
        End If
    End Sub
    Public Class BuildDataSourceClass
        Public Shared connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath("~/Grid/Data/Access/Nwind.mdb")

        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

        Public Shared Function GetDataTable(ByVal query As String, ByVal dbFile As String) As DataTable
            Dim connection1 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath(dbFile))
            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

    End Class
End Class
