>>>>SqlDataReader r = sqlComm.ExecuteReader( CommandBehavior.SingleRow );
>>>>DataGrid1.DataSource = r;你使用的是DataReader,不可以实现分页,建议使用DataSet

解决方案 »

  1.   

    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex=e.NewPageIndex;
    try 

    myConnection.Open(); SqlDataReader r = sqlComm.ExecuteReader( CommandBehavior.SingleRow );
    DataGrid1.DataSource = r;
    DataGrid1.DataBind();                                                                      
    r.Close();

    }
    catch (Exception ex)
    {
    string cc = ex.Message;
    }
    finally 
    {

    myConnection.Close();
    } }
      

  2.   

    TO rainy6669 () :请用DataSet替换SqlDataReader
      

  3.   

    <%@ Page Language="C#" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.SqlClient" %>
    <script runat="server">    void Page_Load(object sender, EventArgs e) {
        
            if (!Page.IsPostBack) {
        
                // Databind the data grid on the first request only
                // (on postback, rebind only in paging command)
        
                BindGrid();
            }
        }
        
        void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {
        
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
        
        void BindGrid() {
        
            // TODO: update the ConnectionString and CommandText values for your application
            string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
            string CommandText = "select au_lname, au_fname, address, city, state from Authors order by au_lname";
        
            SqlConnection myConnection = new SqlConnection(ConnectionString);
            SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
        
            DataSet ds = new DataSet();
            myCommand.Fill(ds);
        
            DataGrid1.DataSource = ds;
            DataGrid1.DataBind();
        }</script>
    <html>
    <head>
    </head>
    <body style="FONT-FAMILY: arial">
        <h2>Data Report with Paging 
        </h2>
        <hr size="1" />
        <form runat="server">
            <asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="6" AllowPaging="true">
                <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
                <PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
                <ItemStyle backcolor="#DEDFDE"></ItemStyle>
            </asp:datagrid>
        </form>
    </body>
    </html>
      

  4.   

    首先右键DataGrid控件选属性生成器->分页 ,选择允许分页
    再给DataGrid控件添加PageIndexChanged事件;
    代码为:
      private void BindGride()
    {
                this.sqlConnection1.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["car"];
    string CommandString = "";
    CommandString = "select * from dpxxb order by datetime desc ";

    this.sqlConnection1.Open ();
    DataSet dataSet1=new DataSet ();
    SqlDataAdapter myCommane = new SqlDataAdapter(CommandString,sqlConnection1);
    myCommane.Fill(dataSet1,"tabmsg");
    this.DataGrid1.DataSource = dataSet1.Tables["tabmsg"].DefaultView;
    DataGrid1.DataBind();
    this.sqlConnection1 .Close ();
    }
    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
    BindGride();
    }