• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Datagrid Paging - Not paging past second page

I have a datagrid that only pages to the second page, even though there is enough data to fill atleast 20 or so pages.  Below is the datagrid & OnPageIndexChanged sub:

<ASP:DataGrid id="MyDataGrid" runat="server"
    Width="855"
    BackColor="#cccccc"
    BorderColor="#FFFFFF"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana" Height="16px" BorderWidth="2"
    Font-Size="11px"
    HeaderStyle-BackColor="#999999" HeaderStyle-Height="16px" HeaderStyle-Font-Size="11px"
    EnableViewState="false"
    HorizontalAlign="Center"
    AllowPaging="true"
    PageSize="8"  
    AllowCustomPaging="false"
    OnPageIndexChanged="MyDataGrid_PageIndChange"
/>

  Sub MyDataGrid_PageIndChange(s As Object, e As DataGridPageChangedEventArgs)
      MyDataGrid.CurrentPageIndex = e.NewPageIndex
      BindDataGrid
  End Sub

  Sub BindDataGrid
     Dim dr1 As SqlDataAdapter
     Dim ds1 As DataSet
     dr1 = New SqlDataAdapter(query, MyConnection)
     ds1 = New DataSet
     dr1.Fill(ds1)
            
     MyDataGrid.DataSource = ds1
     MyDataGrid.DataBind()
End Sub

BindDataGrid is also called in the page_load event.  If i place this in 'If Not Page.IsPostBack' the second page does not contain any data at all so I have BindDataGrid occuring in every page_load.  Looking for an answer as to why this dg will not page past the second page.  (since page size is 8 I can only see the first 16 records)

Thanks in advance
0
egdigital
Asked:
egdigital
1 Solution
 
ayha1999Commented:
Hi,

change to

Sub BindDataGrid
     Dim dr1 As SqlDataAdapter
     Dim ds1 As DataSet
     dr1 = New SqlDataAdapter(query, MyConnection)
     ds1 = New DataSet
     dr1.Fill(ds1)

Session("YouVariable")=ds1         <-------------------------- add this line
         
     MyDataGrid.DataSource = ds1
     MyDataGrid.DataBind()
End Sub

Sub MyDataGrid_PageIndChange(s As Object, e As DataGridPageChangedEventArgs)
      MyDataGrid.CurrentPageIndex = e.NewPageIndex
      MyDataGrid.DataSource=Session("YourVariable") <--------- add this
      MyDataGrid.DataBind()       <-------- add this
  End Sub

hope this helps.

ayha
0
 
Jens FiedererTest Developer/ValidatorCommented:
Ayha's solution will allow you NOT to use BindDataGrid in every pageload, only if not page.ispostback
0
 
raterusCommented:
Keep in mind, when you store the data in the session like this, your user isn't going to see any changes someone may be doing to the data until they close their browser.  If this is, was, or ever could be a multi-user app, I wouldn't use this approach.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
egdigitalAuthor Commented:
What is the alternative to using the session approach?  I still am not sure why my original means of paging only went to the second page & not subsequent pages (3, 4, etc)
0
 
raterusCommented:
Yeah, the last posts have nothing to do with your actual problem, my guess is you are rebind the datagrid on a postback, you should have something like this in Page_Load

Sub Page_load(...)
  If Page.IsPostBack Then
    BindDataGrid()
  End If
End Sub
0
 
raterusCommented:
ahh yes, you already mentioned this, then your real problem here is you set enableviewstate=false in the datagrid, you have to set this to true.
0
 
egdigitalAuthor Commented:
Enabling the viewstate & placing the bind in the pageload between 'If Not Page.IsPostBack'  block did the trick.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now