日期:2014-05-18  浏览次数:20515 次

DataList分页
数据库操作用的是LINQ,想用DataList进行分页,不知道该怎么写,还有,顺带问一下,泛型IList<>能直接用(DataTable)强制转换的嘛?求教

------解决方案--------------------
linq分页本来就很简单啊 里面有个wake和skip的方法可以解决你分页的问题
还有想用datatable来转换其实是可以了,要你相应的数据库的实体类
,具体转换可以看
C# code

#region DataTable To List/Model


        /// <summary>
        /// DataTable To List
        /// </summary>
        /// <typeparam name="TType">object type</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns>return a List Model type</returns>
        public static List<T> DataTableToObjectList<T>(DataTable dt) where T : new()
        {
            DataRowCollection drc = dt.Rows;
            int columncount = drc.Count;
            List<T> result = new List<T>();    //declare the generic type of return 
            Type type = typeof(T);
            
            PropertyInfo[] propertys = type.GetProperties(BindingFlags.IgnoreCase|BindingFlags.Instance|BindingFlags.Public|BindingFlags.SetProperty);   //get the collections of the model
            foreach (DataRow r in drc)
            {
                result.Add(DataRowToObjectModel<T>(r, propertys));
            }     
            return result;
            
        }
        
        /// <summary>
        /// DataRow To a Model
        /// </summary>
        /// <typeparam name="T">the type of Model</typeparam>
        /// <param name="r">DataRow</param>
        /// <param name="propertys">the object to Model</param>
        /// <returns>return a Model Type</returns>
        private static T DataRowToObjectModel<T>(DataRow r, PropertyInfo[] propertys) where T : new()
        {
            T t = new T();
            for (int i = 0; i < propertys.Length; i++)
            {
                object obj = r[propertys[i].Name];
                if (obj != null)
                {
                    if (propertys[i].PropertyType == typeof(int))
                        propertys[i].SetValue(t, PublicMethod.GetInt(obj), null);
                    if (propertys[i].PropertyType == typeof(string))
                        propertys[i].SetValue(t, obj.ToString(), null);
                    if (propertys[i].PropertyType == typeof(DateTime))
                        propertys[i].SetValue(t, PublicMethod.GetDateTime(obj), null);
                }
            }
            return t;
        }
        #endregion

------解决方案--------------------
泛型IList<>不能直接用(DataTable)强制转换、IList<>是以键值对的形式存在的。
------解决方案--------------------
int pageCount;
int currentPage = 1;

DataView objView = dt.DefaultView; //表示自定义视图
PagedDataSource pds = new PagedDataSource();//表示实例化 分页
pds.DataSource =objView;
pds.AllowPaging = true;允许分页
pds.PageSize = 4;


pageCount = pds.PageCount;//获取页总数
this.Label4.Text = Convert.ToString(pageCount);


pds.CurrentPageIndex = currentPage - 1;

this.Label2.Text = Convert.ToString(currentPage);

 

this.DataList1.DataSource = pds;
this.DataList1.DataBind();

------解决方案--------------------
private void bind(int n, int m)//举个例子:用linq读取数据库中第n页的前m条记录
{
if (n < 0 || n > count)
return;
DataClassesDataContext dc = new DataClassesDataContext("server=.; integrated security=true;database=url");
var source = (from temp in dc.Class1
orderby temp.id
select new
{