what is your paging code behind? you need to provide a virtual record count

解决方案 »

  1.   

    public void dgrdTopicList_PageIndexChanged(Object s,DataGridPageChangedEventArgs e)
           {
                intStartIndex = e.NewPageIndex * dgrdTopicList.PageSize ;
                dgrdTopicList.CurrentPageIndex = e.NewPageIndex;
                Response.Write("Changed?");
                BindDataGrid();
           }
    public void BindDataGrid()       
           {  
                
           
                intEndIndex = intStartIndex + dgrdTopicList.PageSize;
                
                queryString = "SELECT [Topic].* FROM [Topic] WHERE ([Topic].[TopicCatalog] = @TopicCatalog ) and (ID > @startIndex) And (ID <= @endIndex) ORDER BY subtime desc";
      
            
                SqlCommand sqlCommand = new SqlCommand(queryString);
            
                sqlCommand.Parameters.Add("@TopicCatalog", System.Data.SqlDbType.Int).Value =0;
                sqlCommand.Parameters.Add("@startIndex", System.Data.SqlDbType.Int).Value =intStartIndex;
                sqlCommand.Parameters.Add("@endIndex", System.Data.SqlDbType.Int).Value =intEndIndex;
                
                DataSet dataSet;
            
                DataObj.DataSetQueryCmd(ref sqlCommand,out dataSet,"TopicList");
            
                dgrdTopicList.DataSource=dataSet;
            
                dgrdTopicList.DataBind();
                
           }
    public void Page_Load()
            {
                
                    queryString = "SELECT [Topic].* FROM [Topic] WHERE ([Topic].[TopicCatalog] = @TopicCatalog ) ORDER BY subtime desc";
            
                    SqlCommand sqlCommand = new SqlCommand(queryString,DataObj.conn);
            
                    sqlCommand.Parameters.Add("@TopicCatalog", System.Data.SqlDbType.Int).Value =0;
            
                    
                    dgrdTopicList.VirtualItemCount = ( (int)sqlCommand.ExecuteScalar() / dgrdTopicList.PageSize );
                    
                    
                    
                    BindDataGrid();
                 
           }   
    DataObj是我自己写的类的对象,用来数据库连接的
      

  2.   

    saucer:Response.Write("Changed?");//这句话没被执行
      

  3.   

    why (int)sqlCommand.ExecuteScalar() / dgrdTopicList.PageSize ???see
    数据分页的多种实现方法及性能比较
    http://www.ccw.com.cn/htm/center/prog/02_4_4_3_2.asp
      

  4.   

    here is an example:<%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <html><head>
    <script language="C#" runat="server">
        const String sConn = "Server=localhost;Database=NorthWind;UID=sa;PWD=;";
        SqlConnection oConn= new SqlConnection(sConn);    void Page_Unload(object o, EventArgs e)
        {
    oConn.Close();
        }    void Page_Load(Object o, EventArgs e) 
        {
    oConn.Open();
            if (!IsPostBack) 
            {
                
        SqlCommand cmd = new SqlCommand("select count(*) from orders", oConn);
                MyDataGrid.VirtualItemCount=(int)cmd.ExecuteScalar();     BindGrid(0);
            }
        }    void MyDataGrid_ChangePage(Object o, DataGridPageChangedEventArgs e) 
        {
            MyDataGrid.CurrentPageIndex = e.NewPageIndex;
            BindGrid(e.NewPageIndex);
        }    void BindGrid(int nPageIndex) 
        {
    int nStartIndex = nPageIndex * MyDataGrid.PageSize;
    int nEndIndex = nStartIndex + MyDataGrid.PageSize - 1;
    String sSQL = "select * from (select top " + MyDataGrid.PageSize + " * from (select top " + (nEndIndex+1) + " * from orders order by orderid) t order by orderid desc) t2 order by orderid";
    SqlCommand cmd = new SqlCommand(sSQL, oConn);
            MyDataGrid.DataSource = cmd.ExecuteReader();
            MyDataGrid.DataBind();   
        }
    </script>
    <style type="text/css">BODY {font-family:Verdana}</style>
    </head>
    <body>
        <h3>Custom Paging with DataGrid</h3>
        <form runat=server>
          <ASP:DataGrid id="MyDataGrid" runat="server"
            AllowPaging="True"
            AllowCustomPaging="True"
            PageSize="10"
            PagerStyle-Mode="NextPrev"
    PagerStyle-NextPageText="Forward"
    PagerStyle-PrevPageText="Back"
            PagerStyle-HorizontalAlign="Right"
            OnPageIndexChanged="MyDataGrid_ChangePage"
            BorderColor="black"
            BorderWidth="1"
            GridLines="Both"
            CellPadding="3"
            CellSpacing="0"
            Font-Name="Verdana"
            Font-Size="8pt"
            HeaderStyle-BackColor="#aaaadd"
            AlternatingItemStyle-BackColor="#eeeeee"
            />
      </form>
    </body>
    </html>