• DataTable数据分页


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;

    namespace ConsoleApplication16
    {
    public static class ListAndDataTableExtension
    {
    public static DataTable ToDataTable<T>(this List<T> list) where T : new()
    {
    DataTable table = new DataTable();
    PropertyInfo[] ps = typeof(T).GetProperties();
    ps.ToList().ForEach(p => {
    if (!p.PropertyType.IsGenericType)
    {
    table.Columns.Add(p.Name, p.PropertyType);
    }
    else
    {
    if (p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
    {
    table.Columns.Add(p.Name,Nullable.GetUnderlyingType(p.PropertyType));
    }
    }

    });
    list.ForEach(obj => {
    DataRow row = table.NewRow();
    ps.ToList().ForEach(p => {
    row[p.Name] = p.GetValue(obj);
    });
    table.Rows.Add(row);
    });
    return table;
    }
    public static List<T> ToList<T>(this DataTable table) where T : new()
    {
    List<T> list = new List<T>();
    PropertyInfo[] ps = typeof(T).GetProperties();
    foreach (DataRow row in table.Rows)
    {
    T obj = new T();
    foreach (DataColumn col in table.Columns)
    {
    ps.ToList().ForEach(p => {
    if (p.Name == col.ColumnName)
    {
    if (!p.PropertyType.IsGenericType)
    {
    p.SetValue(obj, string.IsNullOrEmpty(row[p.Name].ToString()) ? null : Convert.ChangeType(row[p.Name], p.PropertyType));
    }
    else
    {
    if (p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
    {
    p.SetValue(obj,string.IsNullOrEmpty(row[p.Name].ToString())?null:Convert.ChangeType(row[p.Name],Nullable.GetUnderlyingType(p.PropertyType)));

    }

    }
    }
    });

    }
    list.Add(obj);
    }
    return list;
    }
    }

    public class Studen
    {
    public int? StuNo { get; set; }
    public string Name { get; set; }
    public string Sex { get; set; }
    public int Age { get; set; }
    public override string ToString()
    {
    string s = string.Empty;
    PropertyInfo[] ps = typeof(Studen).GetProperties();
    ps.ToList().ForEach(p => { s += p.Name + " " + p.GetValue(this).ToString(); });
    return s;
    }
    }
    class Program
    {

    public static List<Studen> stuList = new List<Studen>();
    static void Main(string[] args)
    {
    for (int i = 1; i <=14; i++)
    {
    stuList.Add(new Studen() { StuNo=i,Name=i.ToString(),Sex=i.ToString(),Age=i});
    }
    DataTable table = stuList.ToDataTable<Studen>();
    List<Studen> stus = table.ToList<Studen>();
    DataTable table2 = table.Clone();
    DataTable table3 = table.Clone();
    int rows = table.Rows.Count;
    int pageSize = 5;
    int pages = rows / pageSize;
    foreach (DataRow oldRow in table.Rows)
    {
    DataRow NewRow = table3.NewRow();
    NewRow.ItemArray = oldRow.ItemArray;
    table3.Rows.Add(NewRow);
    }
    if (rows <= pageSize)
    {
    //分页插入的时候每次清空上次的数据
    table2.Rows.Clear();
    for (int i = 1; i <= rows; i++)
    {
    DataRow oldRow = table.Rows[i - 1];
    DataRow newRow = table2.NewRow();
    newRow.ItemArray = oldRow.ItemArray;
    table2.Rows.Add(newRow);
    }
    Console.WriteLine("第一页的数据如下:");
    List<Studen> students = table2.ToList<Studen>();
    students.ForEach(obj => { Console.WriteLine(string.Format(obj.ToString())); });
    //这里行bulkcopy的插入语句直接传入DataTable
    }
    else
    {
    for (int pageIndex = 1; pageIndex <=pages; pageIndex++)
    {
    Console.WriteLine(string.Format("第{0}页数据如下:",pageIndex));
    //分页插入的时候每次清空上次的数据
    table2.Rows.Clear();
    for (int j = (pageIndex - 1) * pageSize + 1; j <= pageIndex * pageSize; j++)
    {

    DataRow oldRow = table.Rows[j - 1];
    DataRow newRow = table2.NewRow();
    newRow.ItemArray = oldRow.ItemArray;
    table2.Rows.Add(newRow);

    Console.WriteLine(table.ToList<Studen>()[j-1].ToString());
    //这里行bulkcopy的插入语句直接传入DataTable
    }



    }
    if (rows % pageSize != 0)
    {
    //分页插入的时候每次清空上次的数据
    table2.Rows.Clear();
    int skip = rows % pageSize;
    Console.WriteLine(string.Format("最后一页数据如下:"));
    for (int j = pages * pageSize + 1; j <= pageSize * pages + skip; j++)
    {

    DataRow oldRow = table.Rows[j - 1];
    DataRow newRow = table2.NewRow();
    newRow.ItemArray = oldRow.ItemArray;
    table2.Rows.Add(newRow);
    Console.WriteLine(table.ToList<Studen>()[j - 1].ToString());
    //这里行bulkcopy的插入语句直接传入DataTable
    }


    }

    }

    #region 大小写间隔 转换成大写加下划线
    string s = "AbcDefGhi";
    s = Regex.Replace(s, @"([A-Z]{1})([a-z]*)", MatchEval);
    s = s.TrimEnd('_');
    #endregion


    #region 大写下划线 转换成 大小写间隔
    string s2 = "ABC_EDF_FSD";
    List<string> strList = s2.Split('_').ToList();
    s2 = "";
    foreach (string str in strList)
    {
    s2 += Regex.Replace(str, @"([A-Za-z]{1})([A-Za-z]*)", MatchEvalEntity);
    }
    #endregion

    Console.ReadKey();
    }

    private static string MatchEvalEntity(Match match)
    {
    return match.Groups[1].Value.ToUpper() + match.Groups[2].Value.ToLower();
    }

    private static string MatchEval(Match match)
    {
    return match.Groups[1].Value.ToUpper() + match.Groups[2].Value.ToUpper()+"_";
    }
    }
    }

  • 相关阅读:
    Web前端学习第五天——————HTML篇.019页面布局练习
    1111
    开发者如何利用数据分析提高收入
    开发者进行广告合作的几大误区
    高仿人人Android梦想版终极源码发送(转)
    移动开发者如何获取免费流量
    Inno setup常用代码【收藏】
    Inno setup常用代码补充【收藏】
    QT for Window程序部署
    Inno Setup自定义卸载文件名称【收藏】
  • 原文地址:https://www.cnblogs.com/kexb/p/4557034.html
Copyright © 2020-2023  润新知