c#分页增加顺序编号方法代码
public List<TModel<T>> ConvertList<T>(int pg, int pagesize, List<T> list,string kidName = "KID") { if (pg < 1) { pg = 1; } List<TModel<T>> models = new List<TModel<T>>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { TModel<T> model = new TModel<T>(); int m = i + 1; var t = list[n].GetType().GetProperty(kidName); if(t != null) { t.SetValue(list[n], m, null); } model.keyid = m; model.propert = list[n]; models.Add(model); n++; } return models; } public List<T> ConvertList2<T>(int pg, int pagesize, List<T> list, string kidName = "KID") { if (pg < 1) { pg = 1; } List<T> models = new List<T>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { int m = i + 1; var t = list[n].GetType().GetProperty(kidName); if (t != null) { t.SetValue(list[n], m, null); } models.Add(list[n]); n++; } return models; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pg">当前页码</param> /// <param name="pagesize">每页条数</param> /// <param name="list">集合数据</param> /// <param name="pagenum">数据总数</param> /// <param name="kidName">自增列名称</param> /// <returns></returns> public List<TModel<T>> ResvertList<T>(int pg, int pagesize, List<T> list, int pagenum, string kidName = "KID") { if (pg < 1) { pg = 1; } List<TModel<T>> models = new List<TModel<T>>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { TModel<T> model = new TModel<T>(); int c = pagenum - i; var t = list[n].GetType().GetProperty(kidName); if (t != null) { t.SetValue(list[n], c, null); } model.keyid = c; model.propert = list[n]; models.Add(model); n++; } return models; } public List<T> ResvertList2<T>(int pg, int pagesize, List<T> list, int pagenum, string kidName = "KID") { if (pg < 1) { pg = 1; } List<T> models = new List<T>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { TModel<T> model = new TModel<T>(); int c = pagenum - i; var t = list[n].GetType().GetProperty(kidName); if (t != null) { t.SetValue(list[n], c, null); } models.Add(list[n]); n++; } return models; }
public class TModel<T> { public int keyid { get; set; } public T propert { get; set; } }
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利
如有需要可联系(qq:2831804923 / 微信号:wh0612910)
----------------------------------------------------------------------------------------------------------------------
愿本文对阁下有帮助,在下谢之:
如有需要可联系(qq:2831804923 / 微信号:wh0612910)
----------------------------------------------------------------------------------------------------------------------
愿本文对阁下有帮助,在下谢之: