Datagrid paging

  • Thread starter Thread starter enak
  • Start date Start date
E

enak

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
 
enak said:
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...
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.DataGridPageChangedEventArgs) 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.DataGridSortCommandEventArgs) 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
 
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
 
Back
Top