0

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  "


mycon.Open()

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


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

GridView1.DataSource = kdt
3
Contributors
2
Replies
14
Views
2 Years
Discussion Span
Last Post by JerrimePatient
0

Hi

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

        adapter.Fill(kdt)

        GridView1.DataSource = kdt

        GridView1.AllowPaging = True
        GridView1.DataBind()

    End Using

End Sub

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

    GridView1.PageIndex = e.NewPageIndex
    BindGridView()

End Sub

HTH

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.