新手上路遇到点麻烦,请高手支招,谢谢问题如下:使用Gridview提示 数据源不支持服务器端的数据分页以下是我的代码
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            String strContent;
            strContent ="%"+this.txtContent.Text+"%";
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ResourceWebConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;                cmd.CommandText = "select * from Visit where "+this.ddlList.Text+" like @Content";
                cmd.Parameters.AddWithValue("@Content", strContent);
                conn.Open();                SqlDataReader dr = cmd.ExecuteReader();                this.GridView1.DataSource = dr;
                //绑定
                this.GridView1.DataBind();                conn.Close();
            }
        }在网上查了下,好象是说SqlDataReader不支持分页,要换用DataSet,因为刚开始学习不熟悉,想请教下以上我写的查询代码该如何改,谢谢

解决方案 »

  1.   

    可以通过SqlDataReader将数据库中的数据存到一个DataTable中
      

  2.   

    不知道谁给你说的SqlDataReader不支持分页,分页和他有半毛钱关系么? 本来要给你贴代码的,csdn提示代码过长
      

  3.   

    这样试试:SqlDataReader dr = cmd.ExecuteReader();
    DataTable dtInfo = new DataTable();
    object[] objValues = new object[dr.FieldCount];
    while (dr.Read())
    {
         dr.GetValues(objValues);
         dtInfo.LoadDataRow(objValues, true);
    }
    GridView1.DataSource = dtInfo;
    GridView1.DataBind();
      

  4.   

    用SqlDataAdapter代替SqlDataReader 
      

  5.   

    protected void btnSearch_Click(object sender, EventArgs e)
      {
      String strContent;
      strContent ="%"+this.txtContent.Text+"%";
      string connString = System.Configuration.ConfigurationManager.ConnectionStrings["ResourceWebConnectionString"].ConnectionString;
      using (SqlConnection conn = new SqlConnection(connString))
      {
      SqlCommand cmd = new SqlCommand();
      cmd.Connection = conn;  cmd.CommandText = "select top 10 *from  Visit where "+this.ddlList.Text+" like @Content and id not in(select top 10*"+(i-1).Tostring()+" id from visit)";   //i是根据页面的textbox传入的值,表示当前页
      cmd.Parameters.AddWithValue("@Content", strContent);
      conn.Open();  SqlDataReader dr = cmd.ExecuteReader();  this.GridView1.DataSource = dr;
      //绑定
      this.GridView1.DataBind();  conn.Close();
      }
      }
      

  6.   

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "";
    cmd.Connection = con;
    SqlDataAdapter sda = new SqlDataAdapter();
    sda.SelectCommand = cmd;
    DataSet ds = new DataSet();
    sda.Fill(ds);
    this.GridView1.DataSource = ds.Tables[0];
      

  7.   

    gridview 不是有自带的分页吗,用起来很方便啊!