编译器错误消息: CS0266: 无法将类型“System.Data.DataTable”隐式转换为“System.Collections.IEnumerable”。存在一个显式转换(是否缺少强制转换?)源错误: 行 31:     private void RefreshStatus()
行 32:     {
行 33:         pds.DataSource = DataHelp.Lights().GetAllList();
行 34:         DataList1.DataSource = pds;行 35:         DataList1.DataBind();
 
分页代码:
public partial class Front_Default : System.Web.UI.Page

    //控件开发人员需对自定义数据绑定控件提供分页支持时,可使用的类PagedDataSource
    public static PagedDataSource pds = new PagedDataSource();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            pds.AllowPaging = true;//允许分页
            pds.CurrentPageIndex = 0;//当前页的索引设置为0
            pds.PageSize = 4;//当也显示项数
        }
        RefreshStatus();
    }
    //流程:1.先在页面加载的时候,把PagedDataSource的参数设置好(如单页显示多少条纪录)
    //      2.pds.DataSource = AdminManager.GetAllAdmins();这样就变成有分页的数据了,然后再和DataList绑定就好了
    private void RefreshStatus()
    {
        pds.DataSource = DataHelp.Lights().GetAllList();
        DataList1.DataSource = pds;
        DataList1.DataBind();
        btnFirst.Enabled = !pds.IsFirstPage;//判断当前页是不是第一页,如果是,那么"第一页"的按钮就应该不可用
        btnPrevious.Enabled = !pds.IsFirstPage;//判断当前页是不是第一页,如果是,那么"上一页"的按钮就应该不可用
        btnNext.Enabled = !pds.IsLastPage;
        btnLast.Enabled = !pds.IsLastPage;
        lblPageIndex.Text = Convert.ToString(pds.CurrentPageIndex + 1);//如果当前页是第一页,pds.CurrentPageIndex的值会为0,所以要+1
        lblPageCount.Text = pds.PageCount.ToString();
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = 0; //如果是第一页,就把pds.CurrentPageIndex参数设置为0,然后在绑定
        RefreshStatus();
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex = pds.PageCount - 1;
        RefreshStatus();
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex += 1;
        RefreshStatus();
    }
    
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        pds.CurrentPageIndex -= 1;
        RefreshStatus();
    }
}

解决方案 »

  1.   

     public void bind()
        {
            int CurrentPage = Convert.ToInt32(labNowPage.Text);
            PagedDataSource ps = new PagedDataSource();
            SqlConnection con = DB.GetCon();
            con.Open();
            string str = "select *from cost";
            SqlCommand mycmd = new SqlCommand(str, con);
            SqlDataAdapter da = new SqlDataAdapter(mycmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "cost");        ps.DataSource = ds.Tables["cost"].DefaultView;
            ps.AllowPaging = true;
            ps.PageSize = 8;
            ps.CurrentPageIndex = CurrentPage - 1; //获取当前页码。
            lnkbtnFront.Enabled = true;
            LinkbtFirst.Enabled = true;
            LnkbtnLast.Enabled = true;
            LnkbtnNext.Enabled = true;
            if (CurrentPage == 1)
            {
                LinkbtFirst.Enabled = false;
                lnkbtnFront.Enabled = false;
            }
            if (CurrentPage == ps.PageCount)
            {
                LnkbtnNext.Enabled = false;
                LnkbtnLast.Enabled = false;
            }        this.labCount.Text = Convert.ToString(ps.PageCount);
            this.GridView1.DataSource = ps;
            this.GridView1.DataKeyNames = new string[] {"c_id"};
            GridView1.DataBind();
            Session["costDataSet"] = ds;
       
        }
        //首页。
        protected void LinkbtFirst_Click(object sender, EventArgs e)
        {
            this.labNowPage.Text = "1";
            this.bind();
        }
        //上一页。
        protected void lnkbtnFront_Click(object sender, EventArgs e)
        {
            this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) - 1);
            this.bind();
        }
        //下一页。
        protected void LnkbtnNext_Click(object sender, EventArgs e)
        {
            this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) + 1);
            this.bind();
        }
        //尾页。
        protected void LnkbtnLast_Click(object sender, EventArgs e)
        {
            this.labNowPage.Text = this.labCount.Text;
            this.bind();
        }
        //全选与全不选。
        protected void Button6_Click(object sender, EventArgs e)
        {
            bool bAllSelect = true;
            //第一次触发全选
            if (this.Button6.Text == "全选")
            {
                bAllSelect = true;
                Button6.Text = "全不选";
            }
            else
            {
                bAllSelect = false;
                Button6.Text = "全选";
            }
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("chkCheck");
                if (bAllSelect)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }
        //删除。
        protected void Button7_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView1.Rows[i].FindControl("chkCheck");            if (cb.Checked)
                {
                    string strCid = this.GridView1.DataKeys[i].Value.ToString();
                    if (BLL.costBll.dCost(strCid))
                    {
                        Response.Write("<script>alert('删除" + strCid + "成功!')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('请选择!')</script>");
                    }
                }
               
            }
            this.bind();
        }   
        protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            this.bind();
        }
        protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            this.bind();
        }
        protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
        {
            string c_id = GridView1.DataKeys[e.RowIndex].Value.ToString();        string c_user = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
            string c_stardate = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
            string c_costName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
            string c_price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
            string c_real = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
            string c_costMoney = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
            string c_userName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString();
            string c_res = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString();        string strSql = "update cost set c_user='" + c_user + "',c_stardate='" + c_stardate + "',c_costName='" + c_costName + "',c_price='" + c_price + "',c_real='" + c_real + "',c_costMoney='" + c_costMoney + "',c_userName='" + c_userName + "',c_res='" + c_res + "' where c_id='" + c_id + "'";
            SqlConnection con = DB.GetCon();
            con.Open();
            SqlCommand cmd = new SqlCommand(strSql, con);
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                RegisterStartupScript("", "<script>alert('修改成功')</script>");
            }
            else
            {
                RegisterStartupScript("", "<script>alert('修改失败')</script>");
            }
            cmd.Dispose();
            con.Close();
            GridView1.EditIndex = -1;
            this.bind();
        }
    你慢慢看下。。
      

  2.   

    pds.DataSource = DataHelp.Lights().GetAllList();
    GetAllList方法返回Datatable,无法转换
    修改为下面试试吧
    Datatable table=DataHelp.Lights().GetAllList();
    var data=from data2 in table.AsEnumerable() select data2;
    DataList1.DataSource=data;
    DataList1.DataBind();
      

  3.   

    PagedDataSource的DataSource需要一个实现了 System.Collections.IEnumerable 的对象
    DataList的DataSource可以是一个 IEnumerable 或 IListSource
    DataTable类只实现了IListSource接口,没有实现IEnumerable接口
      

  4.   

     用 AspNetPager.dll 吧
      上网下一个 添加到引用里
        
      页面
    <%@ register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %><webdiyer:aspnetpager ID="anp" runat="server" AlwaysShow="True" FirstPageText="第一页"
                    LastPageText="最后一页" NextPageText="下一页" OnPageChanged="anp_Pager_PageChanged"
                    PageSize="8" PrevPageText="上一页" ShowBoxThreshold="2">
                </webdiyer:aspnetpager>后台
    private void Bind()
            {
                DataTable dt = new DataTable();
                dt.Load(绑定的数据);//因为返回是sqldatareader所以转换
                ds.Tables.Add(dt);
                //设置分页   
                anp.RecordCount = ds.Tables[0].DefaultView.Count; //记录总数   
                PagedDataSource pd = new PagedDataSource();
                pd.DataSource = ds.Tables[0].DefaultView;
                pd.AllowPaging = true;      //数据源允许分页   
                pd.CurrentPageIndex = anp.CurrentPageIndex - 1;   //显示当前页   
                pd.PageSize = anp.PageSize;   //取控件的分页大小   
                绑定数据控件名称.DataSource = pd;
               绑定数据控件名称.DataBind();
            }
    protected void anp_Pager_PageChanged(object sender, EventArgs e)
            {
    Bind();
            }我一直用这个感觉还可以