472,978 Members | 2,381 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 software developers and data experts.

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 2184

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RelaxoRy | last post by:
sqlConnection1.Open(); myReader = sqlCommand1.ExecuteReader(); DataGrid1.DataSource = myReader; DataGrid1.DataBind(); myReader.Close(); sqlConnection1.Close(); The Datagrid populates fine. ...
3
by: Joseph D. DeJohn | last post by:
I am trying to get pagination working on a datagrid. Can anyone point me to a resource for help on this? I'm not sure if custom paging is the best option or not.
2
by: RJN | last post by:
Hi Sorry for posting again. I have a datagrid which is put inside a div tag to make it scrollable. I need to page the datagrid. The page numbers appear at the bottom of the datagrid and has...
6
by: Shawn | last post by:
Hi. I already have a datagrid where I'm using paging. I have a stored procedure that fills a temp table with 200-500 rows and then sends back 10 records at the time. When I go to page 2 the SP...
5
by: quanga | last post by:
Hi- I have a datagrid that in some cases can be several thousand rows in length. What I'd like it to do is have the data pushed to the browser after every, say, 100 rows or so. Kind of a...
2
by: Axel Dahmen | last post by:
Hi, I'm using a DataGrid control to show a table's content with paging. For navigation through the pages I'm using the DataGrid's intrinsic navigation section. My problem: The DataGrid...
5
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go...
0
by: Pat | last post by:
I have 3 Datagrid nested. Master Details Child The master has paging (And i'm using the paging inbuilt in the Datagrid) in the Master DataGrid you select a linkbutton(using commandname) and it...
3
by: Pat | last post by:
I have a 2 nested Datagrid. When i select a row in the Master Datagrid i populate the Child databrid using myDataGrid.SelectedIndex value as the filter and setting the DataKeyField. I enabled...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.