我没有为GridView中绑定数据源,而是用代码实现的搜索,但是他就不支持分页啦
System.NotSupportedException: 数据源不支持服务器端的数据分页。
怎么弄。
我是用void showAll()方法绑定显示的数据 void showAll()
    {
        SqlConnection conn = new SqlConnection();
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();        cmd.Connection = conn;
        cmd.CommandText = "Select MP_ID,MP_Name,MP_Address,MP_Phone,MP_Email from MO_MP";
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();        this.myGrid.DataSource = reader;
        this.myGrid.DataBind();        reader.Close();
        conn.Close();
    }
而且用的代码实现搜索
protected void MP_Search_Click(object sender, EventArgs e)
    {
        string key = this.MP_txtKey.Text.Trim();        if (key != "")
        {
            SqlConnection conn = new SqlConnection();
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select MP_Name,MP_Address,MP_Phone,MP_Email from MO_MP ";
            //按条件搜索
            cmd.CommandText += "where MP_Name like'%" + key + "%' ";
            cmd.CommandText += "or MP_Address like'%" + key + "%' ";
            cmd.CommandText += "or MP_Phone like'%" + key + "%' ";
            cmd.CommandText += "or MP_Email like'%" + key + "%' ";
            cmd.Connection = conn;            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();            this.myGrid.DataSource = reader;
            this.myGrid.DataBind();            reader.Close();
            conn.Close();
        }
        else
        {
            showAll();
        }
    }
就没办法分页啦!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
               }
        public void bind()
        {
            SqlConnection cn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=sa");
            cn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from authors", cn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cn.Close();
            GridView1.DataSource = ds;
            GridView1.AllowPaging = true;
            GridView1.PageSize = 5;
            GridView1.DataBind();
            if (GridView1.PageIndex == 0)
            {
                Button1.Enabled = false;
                Button2.Enabled = false;
            }
            else
            {
                Button1.Enabled = true;
                Button2.Enabled = true;
            }
            if (GridView1.PageIndex == GridView1.PageCount - 1)
            {
                Button3.Enabled = false;
                Button4.Enabled = false;
            }
            else
            {
                Button3.Enabled = true;
                Button4.Enabled = true;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
          
            GridView1.PageIndex = 0;
            bind();
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            GridView1.PageIndex = GridView1.PageIndex + 1;
            bind();
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            GridView1.PageIndex = GridView1.PageCount - 1;
            bind();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            GridView1.PageIndex = GridView1.PageIndex - 1;
            bind();
        }
    }
      

  2.   

    http://blog.csdn.net/QQ1015270553/archive/2011/05/11/6412936.aspx
    这是我写的Repeater分页,大同小异,希望对你有好处. 
      

  3.   

    试试AspNetPager