Webform中DataGrid的自定义分页

Webform里的Datagrid带自动分页的功能,只要设定Pagesize和Allowpage即可。可是,实现方法非常的弱智,每次都要把所有的数据都取来,然后再去计算总页数和当前页数。这样导致的问题是每次绑定/翻页都读一遍整个datasource,大数据量时简直是恶梦。

解决方法是用自定义分页Allowcustompage。以下是Sample Code,主要思路是在Store Procedue最后把结果输出到一个零时表,并加上Identity的字段用作分页用。实现起来比较容易,可以在Store Procedue中加上条件和排序变量。

Datagrid
                     < asp:datagrid  id ="dgResult"  runat ="server"  Font-Names ="Verdana"  Font-Size ="10pt"  Width ="100%"  AllowPaging ="True"  PageSize ="10"     AutoGenerateColumns ="False"   AllowCustomPaging ="True" >
                        
< Columns >
                            
< asp:BoundColumn  DataField ="Name"  HeaderText ="Name" >
                                
< HeaderStyle  Width ="100px" > HeaderStyle>
                            
asp:BoundColumn>
                            
<asp:BoundColumn DataField="Gender" HeaderText="Gender">
                                
<HeaderStyle Width="70px"> HeaderStyle>
                            
asp:BoundColumn>
                        
Columns>
                        
<PagerStyle  Mode="NumericPages"      PageButtonCount="10"> PagerStyle>
                    
asp:datagrid>


BindData

Private   Sub GridBind(ByVal dg As DataGrid)

        
Dim data As New MySpaceData
        
Dim _dtWebs As New DataTable
         'You can put condition paraments in the function
        dg.VirtualItemCount 
= data.getResultNumber() 'Dim the VirtualItemCount for the datagrid 
        _dtWebs = data.getResult(dg.CurrentPageIndex * dg.PageSize + 1, (dg.CurrentPageIndex + 1* dg.PageSize) 

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

getResultNumber
Public   Function getResultNumber() As Int16

        
Try
            comm.CommandText 
= "spGetResultNumber"
            comm.Parameters.Clear()
            
'You can put condition paraments in the function
            comm.Parameters.Add("@recordsNum", SqlDbType.Int)
            comm.Parameters(
"@recordsNum").Direction = ParameterDirection.Output

            
Dim _Result As Int16

            conn.Open()
            comm.ExecuteNonQuery()
            _Result 
= comm.Parameters("@recordsNum").Value
            conn.Close()

            
Return _Result

        
Catch ex As Exception

        
End Try

End Function

getResult
  Public   Function getResult(ByVal startIndex As Int16, ByVal endindex As Int16) As DataTable

        
Try
            comm.CommandText 
= "spGetResult"
            comm.Parameters.Clear()
            
'You can put condition paraments in the function
            comm.Parameters.Add("@startIndex", startIndex)
            comm.Parameters.Add(
"@endIndex", endindex)

            
Dim _Result As New DataTable

            da 
= New SqlClient.SqlDataAdapter
            da.SelectCommand 
= comm

            conn.Open()
            da.Fill(_Result)
            conn.Close()

            
Return _Result

        
Catch ex As Exception

        
End Try

End Function

spGetResultNumber
CREATE   PROCEDURE   [ dbo ] . [ spGetResultNumber ]  

AS  

Begin

Select   RecordID, Name,Gender  into  #tempResult  from  tblSample

Select  @recordsNum = count (RecordID)  From  #tempResult

end
GO

spGetResult

CREATE   PROCEDURE   [ dbo ] . [ spGetResult ]  

@startIndex 
int ,
@endIndex 
int

AS  

Begin

-- New a field PKID for paging
Select    identity ( int , 1 , 1 as  PKID, RecordID, Name,Gender  into  #tempResult  from  tblSample

Select   *   from  #tempResult  where  PKID  between  @startIndex  and  @endIndex 

end
GO

你可能感兴趣的:(datagrid)