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 + "\t" + 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()+"_";
}
}
}

DataTable数据分页的更多相关文章

  1. [.NET] SQL数据分页查询

    [.NET] SQL数据分页查询 程序下载 范例下载:点此下载 原始码下载:点此下载 NuGet封装:点此下载 数据查询 开发系统时,使用C#执行SQL查询指令,就可以从SQL数据库里查询所需数据. ...

  2. C#针对DataTable进行分页方法

    以下的分页方法是针对数据量不是非常大的数据进行的,是在内存中进行的分页操作. /// <summary> /// DataTable分页 /// </summary> /// ...

  3. js 从一个json拼接成另一个json,并做json数据分页table展示

    先给数据: //原始json数据json = [{"id":"1","aid":"013","performa ...

  4. thinkphp5配合datatable插件分页后端处理程序

    thinkphp5配合datatable插件分页后端处理程序第一版DataTable.php v.1.0 <?php use think\Db; /** * DataTable.php. */ ...

  5. ASP.NET真分页_接前篇引用AspNetPager.dll进行数据分页

    一.前端准备工作 1.之前我写到过<Asp.net中引用AspNetPager.dll进行数据分页>  这种分页方式只能在前台将数据分页,而每次点击查询时对目标数据库还是全查询,这样不仅会 ...

  6. mysq大数据分页

    mysql limit大数据量分页优化方法 Mysql的优化是非常重要的.其他最常用也最需要优化的就是limit.Mysql的limit给分页带来了极大的方便,但数据量一大的时候,limit的性能就急 ...

  7. excel to datatable (c#用NPOI将excel文件内容读取到datatable数据表中)

    将excel文件内容读取到datatable数据表中,支持97-2003和2007两种版本的excel 1.第一种是根据excel文件路径读取excel并返回datatable /// <sum ...

  8. DataTable数据批量写入数据库三种方法比较

    DataTable数据批量写入数据库三种方法比较 标签: it 分类: C#1)   insert循环插入:2)   sqldataadapter.update(dataset,tablename); ...

  9. c# applibrary实现一个Sheet表中存放多张DataTable数据

    1.工具类(applibrary.dll) public class ExcelHelper { /// <summary> /// 文件名 /// </summary> pu ...

随机推荐

  1. 【学习记录】二分查找的C++实现,代码逐步优化

    二分查找的思想很简单,它是针对于有序数组的,相当于数组(设为int a[N])排成一颗二叉平衡树(左子节点<=父节点<=右子节点),然后从根节点(对应数组下标a[N/2])开始判断,若值& ...

  2. Spring入门一----HelloWorld

    知识点: 简介 HelloWorld 简介: 百度百科   HelloWorld 项目结构图:      导入Spring支持包: 然后选中所有包,右键Build Path à Add to Buil ...

  3. bean对grub4dos做出的巨大贡献总结

    bean对grub4dos做出的巨大贡献总结 ===================================================================bean对grub4 ...

  4. windows平台最简单的rtmp/hls流媒体服务器

    feature: rtmp/hls live server for windows, double click to run,don't need config. run and quit: doub ...

  5. Django 组件-中间件

    中间件 中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不好 ...

  6. Hibernate学习10——Hibernate 查询方式

    本章主要是以查询Student的例子: Student.java: package com.cy.model; public class Student { private int id; priva ...

  7. 杂项:HTML5-2/3-新元素

    ylbtech-杂项:HTML5-2/3-新元素 自1999年以后HTML 4.01 已经改变了很多,今天,在HTML 4.01中的几个已经被废弃,这些元素在HTML5中已经被删除或重新定义. 为了更 ...

  8. hadoop集群调优-OS和文件系统部分

    OS and File System 根据Dell(因为我们的硬件采用dell的方案)关于hadoop调优的相关说明,改变几个Linux的默认设置,Hadoop的性能能够增长大概15%. open f ...

  9. 使用poi读写Excel------demo

    package com.js.ai.modules.pointwall.interfac; import java.io.FileInputStream; import java.io.FileOut ...

  10. poj-2828 Buy Tickets(经典线段树)

    /* Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10207 Accepted: 4919 Descr ...