有一个简单办法
你在每次调用搜索方法前,先把DataGrid.CurrentPageIndex设置为0;
这样就不会出错了。

解决方案 »

  1.   

    請問在哪個地方設置DataGrid.CurrentPageIndex=0;
      

  2.   

    就是你
    在搜索按钮的事件里调用索索方法后
    你不是要BindData()吗?
    就这个前面
      

  3.   

    是這樣嗎?我這樣之後,它總在第一頁呢?按下頁沒反應。
     public void BindData(){
     myconn=new SqlConnection(conn.connstr);
     myconn.Open();
     mycmd=new SqlCommand("SearchCustomerName",myconn);
     mycmd.CommandType=CommandType.StoredProcedure;
     
     SqlParameter prt1=new SqlParameter("@str1",SqlDbType.Char);
     prt1.Value=txt1.Text;
     mycmd.Parameters.Add(prt1);
     
     SqlParameter prt2=new SqlParameter("@str2",SqlDbType.NVarChar);
     prt2.Value=txt2.Text;
     mycmd.Parameters.Add(prt2);
     
     myda=new SqlDataAdapter(mycmd);
     myds=new DataSet();
     myda.Fill(myds,"table1");
     mygrid1.DataSource=myds.Tables["table1"];
     mygrid1.CurrentPageIndex=0;  
     mygrid1.DataBind();
     }
      

  4.   

    整理後如下:using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace CRM{
         public class CONNStr{
        public string connstr=ConfigurationSettings.AppSettings["AData"];
     }
     
     public class CustomerInfoWeb:System.Web.UI.Page{
               public CONNStr conn=new CONNStr();
       public SqlConnection myconn;
       public SqlCommand mycmd;
       public SqlDataAdapter myda;
       public DataSet myds;
       public DataGrid mygrid1;
       public Label label1;
       public TextBox txt1,txt2;
       public Button button1,button2;
       
      
               public void DataGrid_PageChanged1(object sender,DataGridPageChangedEventArgs e){
      mygrid1.CurrentPageIndex=e.NewPageIndex;
      BindData();
          int pageNO=mygrid1.CurrentPageIndex+1;
          label1.Text="共"+mygrid1.PageCount+"頁,"+mygrid1.PageCount*mygrid1.PageSize+"筆記錄,這是第"+pageNO+"頁";
              }       
      
             public void button1_click(object sender,System.EventArgs e){  
                  mygrid1.CurrentPageIndex=0;
      BindData();
     } 
     
             public void button2_cancel(object sender,System.EventArgs e){
     txt1.Text="";
     txt2.Text="";
     } 
     
     public void BindData(){
     myconn=new SqlConnection(conn.connstr);
     myconn.Open();
     mycmd=new SqlCommand("SearchCustomerName",myconn);
     mycmd.CommandType=CommandType.StoredProcedure;
     
     SqlParameter prt1=new SqlParameter("@str1",SqlDbType.Char);
     prt1.Value=txt1.Text;
     mycmd.Parameters.Add(prt1);
     
     SqlParameter prt2=new SqlParameter("@str2",SqlDbType.NVarChar);
     prt2.Value=txt2.Text;
     mycmd.Parameters.Add(prt2);
     
     myda=new SqlDataAdapter(mycmd);
     myds=new DataSet();
     myda.Fill(myds,"table1");
     mygrid1.DataSource=myds.Tables["table1"];    
     mygrid1.DataBind();
     }
    }
    }