I'm having trouble with my pagination using grid view if the datasource is in Page_Load event.

But when I manually use the GridView -> choose datasource in its property, I don't have any problem with the pagination.

I'm using the Page_Load event because there are some parameters.

Please help and thanks in advance. I'm newbie in ASP.net. I've also attached the code I'm using.

Dim mycon As New SqlConnection("Data Source=xxx.168.14.xxx;Initial Catalog=mydatabase;User ID=myuser;Password=mypassword")
Dim myDataTable As New DataTable
Dim kcmd As New SqlClient.SqlCommand

Dim query As String

query = "select * from myquery  "


kcmd.Connection = mycon
kcmd.CommandTimeout = 0
kcmd.CommandText = query

Dim kda As New SqlClient.SqlDataAdapter(kcmd)
Dim kdt As New DataTable

GridView1.DataSource = kdt


I don't see in your code where you are specifying that you want to allow paging of the GridView?

Anyhow, to do this you need to first state that you want to allow paging and then secondly, you need to handle the PageIndexChanging event of the GridView. So an example would be as follows:

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

    If Not Page.IsPostBack Then BindGridView()

End Sub

Private Sub BindGridView()

    Dim query As String = "SELECT * FROM Test_Table"

    Using adapter As New SqlDataAdapter(query, System.Configuration.ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString)

        Dim kdt As New DataTable


        GridView1.DataSource = kdt

        GridView1.AllowPaging = True

    End Using

End Sub

Private Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging

    GridView1.PageIndex = e.NewPageIndex

End Sub


try to use kendo ui.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.