468,733 Members | 1,695 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

Datagrid paging

I can not get my datagrid to page. I have a datagrid that
I can sort 2 of the columns. This works great. I added
paging and when I display the dg it shows 5 pages. (I am
showing page numbers at the bottom of the dg.)

When I click on the pages nothing happens until I get to
the last page. Then and only then is the last page
displayed. If I go back through the pages the second to
last page displayes the first page again.

As I trace the code through the debugger I can see that
the code is executed correctly but yet the pages don't
switch.

Here is my code:

<asp:datagrid id="dgCostCodes" runat="server"
Width="503px" AutoGenerateColumns="False"
AllowSorting="True"
PageSize="15" AllowPaging="True"
OnPageIndexChanged="dgCostCodes_Page">

.... Template Columns

</asp:datagrid>
CODEBEHIND

Private Sub dgBind()
Dim sSQL As String

If Not ds.Tables("tCostCodes") Is Nothing Then
ds.Tables("tCostCodes").Clear()
End If

sSQL = "select * from tblCostCodes Order By " & viewstate
("SortColumn") & " " & viewstate("AscDesc")

If cDb.getData(sSQL, "tCostCodes", ds) Then
Me.dgCostCodes.DataSource = ds.Tables("tCostCodes")
Me.dgCostCodes.DataBind()
End If

End Sub
Public Sub dgCostCodes_Page(ByVal sender As Object, ByVal
e As DataGridPageChangedEventArgs)
Me.dgCostCodes.CurrentPageIndex = e.NewPageIndex
Call dgBind()
End Sub
Thanks
enak
Nov 18 '05 #1
2 1926

"enak" <an*******@discussions.microsoft.com> wrote in message
news:12*****************************@phx.gbl...
I can not get my datagrid to page. I have a datagrid that
I can sort 2 of the columns. This works great. I added
paging and when I display the dg it shows 5 pages. (I am
showing page numbers at the bottom of the dg.)

When I click on the pages nothing happens until I get to
the last page. Then and only then is the last page
displayed. If I go back through the pages the second to
last page displayes the first page again.

As I trace the code through the debugger I can see that
the code is executed correctly but yet the pages don't
switch.

Here is my code:

<asp:datagrid id="dgCostCodes" runat="server"
Width="503px" AutoGenerateColumns="False"
AllowSorting="True"
PageSize="15" AllowPaging="True"
OnPageIndexChanged="dgCostCodes_Page">

... Template Columns

</asp:datagrid>
Why are you adding your own event handler to the DataGrid? A DataGrid
already has a "PageIndexChanged" event that will fire when the user attempts
to show a different page of data. Your HTML for the DataGrid should look
like this:

<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True">
....Template Columns...
</asp:DataGrid>


CODEBEHIND
You may want to rethink this whole dgBind() Sub. I assume you are calling
this from the Page_Load event, but I hope you are not doing it every time
the page loads. I'm not sure why you are clearing your DataTable in your
DataSet if it exists only to go and get it again but sorted differently
(that is what the DataGrid's SortCommand is all about. You should only be
calling DataBind on your DataGrid the first time the page loads and let
ViewState take it from there.

Private Sub dgBind()
Dim sSQL As String
If Not ds.Tables("tCostCodes") Is Nothing Then
ds.Tables("tCostCodes").Clear()
End If

sSQL = "select * from tblCostCodes Order By " & viewstate
("SortColumn") & " " & viewstate("AscDesc")

If cDb.getData(sSQL, "tCostCodes", ds) Then
Me.dgCostCodes.DataSource = ds.Tables("tCostCodes")
Me.dgCostCodes.DataBind()
End If

End Sub


As I stated earlier, a DataGrid already has a PageIndexChanged event, so why
you are creating one of your own escapes me.
Public Sub dgCostCodes_Page(ByVal sender As Object, ByVal
e As DataGridPageChangedEventArgs)
Me.dgCostCodes.CurrentPageIndex = e.NewPageIndex
Call dgBind()
End Sub


Here's code that will do sorting and paging as well as remember the sort
criteria after doing a page change:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Try
'Connect to your DB and store a copy in a Module Level
DataSet
Catch ConnectError As Exception
'Exception Handling Here
End Try

'Set up the DataGrid
dg.DataSource = DataSet
dg.DataBind()
End If
End Sub

Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEvent Args) Handles
dg.PageIndexChanged
dg.CurrentPageIndex = e.NewPageIndex

'Now that the right page of data is showing, sort it according to
the last known sort criteria
Dim dv As New DataView(dsCusts.Tables(0))
dv.Sort = ViewState.Item("SortBy")
dg.DataSource = dv

dg.DataBind()
End Sub

Private Sub dg_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles
dg.SortCommand
Dim dv As New DataView(dsCusts.Tables(0))

'Do the sort
dv.Sort = e.SortExpression

'Store the field to sort on in case the user wants to change pages
after they sort
ViewState.Add("SortBy", e.SortExpression)

dg.DataSource = dv
dg.DataBind()
End Sub
Nov 18 '05 #2
I tried the PageIndexChanged event handler but got the
same results so I thought that I would try my own.

I am only calling the dgBind when the page loads the first
time. I have looked at many examples to paging and have
followed them to the "T" but I don't get the same results.

enak
-----Original Message-----

"enak" <an*******@discussions.microsoft.com> wrote in messagenews:12*****************************@phx.gbl...
I can not get my datagrid to page. I have a datagrid that I can sort 2 of the columns. This works great. I added
paging and when I display the dg it shows 5 pages. (I am
showing page numbers at the bottom of the dg.)

When I click on the pages nothing happens until I get to
the last page. Then and only then is the last page
displayed. If I go back through the pages the second to
last page displayes the first page again.

As I trace the code through the debugger I can see that
the code is executed correctly but yet the pages don't
switch.

Here is my code:

<asp:datagrid id="dgCostCodes" runat="server"
Width="503px" AutoGenerateColumns="False"
AllowSorting="True"
PageSize="15" AllowPaging="True"
OnPageIndexChanged="dgCostCodes_Page">

... Template Columns

</asp:datagrid>
Why are you adding your own event handler to the

DataGrid? A DataGridalready has a "PageIndexChanged" event that will fire when the user attemptsto show a different page of data. Your HTML for the DataGrid should looklike this:

<asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True"> ....Template Columns...
</asp:DataGrid>


CODEBEHIND
You may want to rethink this whole dgBind() Sub. I

assume you are callingthis from the Page_Load event, but I hope you are not doing it every timethe page loads. I'm not sure why you are clearing your DataTable in yourDataSet if it exists only to go and get it again but sorted differently(that is what the DataGrid's SortCommand is all about. You should only becalling DataBind on your DataGrid the first time the page loads and letViewState take it from there.

Private Sub dgBind()
Dim sSQL As String
If Not ds.Tables("tCostCodes") Is Nothing Then
ds.Tables("tCostCodes").Clear()
End If

sSQL = "select * from tblCostCodes Order By " & viewstate ("SortColumn") & " " & viewstate("AscDesc")

If cDb.getData(sSQL, "tCostCodes", ds) Then
Me.dgCostCodes.DataSource = ds.Tables("tCostCodes")
Me.dgCostCodes.DataBind()
End If

End Sub


As I stated earlier, a DataGrid already has a

PageIndexChanged event, so whyyou are creating one of your own escapes me.
Public Sub dgCostCodes_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
Me.dgCostCodes.CurrentPageIndex = e.NewPageIndex
Call dgBind()
End Sub

Here's code that will do sorting and paging as well as

remember the sortcriteria after doing a page change:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Try
'Connect to your DB and store a copy in a Module LevelDataSet
Catch ConnectError As Exception
'Exception Handling Here
End Try

'Set up the DataGrid
dg.DataSource = DataSet
dg.DataBind()
End If
End Sub

Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e AsSystem.Web.UI.WebControls.DataGridPageChangedEven tArgs) Handlesdg.PageIndexChanged
dg.CurrentPageIndex = e.NewPageIndex

'Now that the right page of data is showing, sort it according tothe last known sort criteria
Dim dv As New DataView(dsCusts.Tables(0))
dv.Sort = ViewState.Item("SortBy")
dg.DataSource = dv

dg.DataBind()
End Sub

Private Sub dg_SortCommand(ByVal source As Object, ByVal e AsSystem.Web.UI.WebControls.DataGridSortCommandEven tArgs) Handlesdg.SortCommand
Dim dv As New DataView(dsCusts.Tables(0))

'Do the sort
dv.Sort = e.SortExpression

'Store the field to sort on in case the user wants to change pagesafter they sort
ViewState.Add("SortBy", e.SortExpression)

dg.DataSource = dv
dg.DataBind()
End Sub
.

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Joseph D. DeJohn | last post: by
2 posts views Thread by RJN | last post: by
6 posts views Thread by Shawn | last post: by
5 posts views Thread by quanga | last post: by
2 posts views Thread by Axel Dahmen | last post: by
3 posts views Thread by Pat | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.