Problem in DataGrid Paging

S

Sujith V. P.

Hi All,

I am facing a small problem of paging in DataGrid.I am binding the
dataset to the datagrid in page load as given below,

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.DataGrid1.AllowPaging = True
Me.DataGrid1.PagerStyle.Mode = PagerMode.NextPrev
Me.DataGrid1.PageSize = 10
Dim Cn As SqlConnection = New
SqlConnection("Server=sss;DataBase=sss;Uid=sa;PWD=sa")
Cn.Open()
Dim sqlAdapter As SqlDataAdapter = New SqlDataAdapter("Select TaskUniqueId
as TaskId,TaskDescription as Description,SubCategory from Tasks", Cn)
Dim dsTasks As New DataSet
sqlAdapter.Fill(dsTasks, "Tasks")
Me.DataGrid1.DataSource = dsTasks.Tables("Tasks")
Me.DataGrid1.DataBind()
Cn.Close()

End Sub

Then for paging am using the PageIndexChanges event of the datagrid as
below,

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged

Me.DataGrid1.CurrentPageIndex = e.NewPageIndex

End Sub

But when i click the next or previous button,the page is not navigated..but
during the second click its working fine..does anyone know this problem?

Waiting for a reply from anyone

Thanks in advance
Sujith V.P
 
M

Michael

First off, this probably would be better answered in
microsoft.public.dotnet.framework.aspnet

But I believe you problem lies in the fact that on every page_load, you are
refilling the dataset, and rebinding to the grid. You need to check to make
sure the page hasn't been posted back to itself, before filling the grid
(You only want to update the grid if they page through it, or if it is the
first request of the page). Here is some code of mine of a working pagable
datagrid

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Load DropDownList1

If Not Page.IsPostBack Then
fillDataGrid1()
End If

End Sub

Private Sub fillDataGrid1()

Dim cmd As New SqlCommand("Select * from Orders", conn)

conn.Open()

Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet

da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
conn.Close()
End Sub

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
fillDataGrid1()
End Sub

HTH,
--Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top