日期:2014-05-19  浏览次数:20518 次

为何DataGrid不能自动分页
页面:
        <asp:DataGrid   ID= "Books "   runat= "server "   AllowCustomPaging= "true "   AllowPaging= "true "   PageSize= "4 "   PagerStyle-Mode= "numericpages "  
        GridLines= "both "   AutoGenerateColumns= "false "   CellSpacing= "0 "   OnPageIndexChanged= "Books_PageIndexChanged "   Width= "750px ">
                <ItemStyle   BackColor= "yellow "/>
                <AlternatingItemStyle   BackColor= "#00CC00 "   />
                <HeaderStyle   Font-Bold= "true "   BackColor= "Red "   />
                <Columns>
                        <asp:BoundColumn   DataField   = "BookID "   HeaderText= "编号 "> </asp:BoundColumn>
                        <asp:BoundColumn   DataField= "Categoryid "   HeaderText= "分类编号 "> </asp:BoundColumn>
                        <asp:BoundColumn   DataField= "BookName "   HeaderText= "书名 "> </asp:BoundColumn>                        
                </Columns>
                <PagerStyle   HorizontalAlign= "right "   Mode= "numericpages "   />
        </asp:DataGrid>

后内代码无误,并且可以得到Books.VirtualItemCount值为10,为什么不能显示出1,2,3,4,5.....这样方式,而只显示一页呢

------解决方案--------------------
帮你顶一下

我从来不用自动分页,还是自己写的分页好。将页码传给存储过程,然后存储过程根据接收到的参数读取出你所需要的数据,很方便的。
declare @page int
select top 10 * from tablename WHERE (key NOT IN (select top CONVERT(varchar(10),(@page-1)*10) key from tablename ORDER BY filldates DESC) ORDER BY filldates DESC

key是主键
filldates是自动生成的时间,你也可以找其他字段做排序标准
------解决方案--------------------
用dataset绑定自动分页会失效,要加一个函数


Private Sub DataGrid1_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid2.PageIndexChanged
If Me.DataGrid2.CurrentPageIndex <= e.NewPageIndex Then
If Me.DataGrid2.CurrentPageIndex < (Me.DataGrid2.PageCount - 1) Then
Me.DataGrid2.CurrentPageIndex = e.NewPageIndex
End If
Else
If (Me.DataGrid2.CurrentPageIndex > 0) Then
Me.DataGrid2.CurrentPageIndex = e.NewPageIndex
End If
End If
Exit Sub
End Sub