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. SpringMVC-Spring-Hibernate项目搭建之一-- 搭建maven 项目 & servlet的demo

    一. 搭建maven项目  1. 新建maven项目,选择maven Project --> Next 2. 勾选 Create a simple project --> Next 3. ...

  2. Web项目中定时任务无法绑定SessionFactory的问题解决

    正常我们在web开发中,由于需要在页面上或者脱离事务时使用到懒加载对应的对象,一般都采用Open Session In View模式.   Open Session In View   OpenSes ...

  3. Linux常用命令英文缩写

    命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户      (ubuntu 使用管理员权限 是 sudo) rpm ...

  4. 【BZOJ】3670: [Noi2014]动物园(KMP)

    题目 传送门:QWQ 分析 像求next一样求num. 第二次求next时加上不超过一半的条件. 时间复杂度: $ \huge O ( n ) $ 代码 // luogu-judger-enable- ...

  5. NodeJS写模块和引入模块的例子

    nodejs自学.js function hello(){ console.log("hello world");} function s(){ console.log(" ...

  6. Disconf实践指南:使用篇

    在上一篇文章Disconf实践指南:安装篇介绍了如何在本地搭建Disconf环境,下面我们介绍如何在项目中使用Disconf.由于某些功能特性对源码做了修改,所以在官方文档并没有提及. 环境基于mac ...

  7. ie6绝对定位的块会被select元素遮挡的解决方案

    RT(已无力吐槽ie),解决方法是:定义一个iframe,与想要显示的绝对定位的块设置为同一大小.放在同一个位置上.我的网页里绝对定位的元素是会随着鼠标移动显示和隐藏的,于是这个frame也要跟着显示 ...

  8. Java-从堆栈常量池解析equals()与==

    一.基本概念 ①JAVA中的基本数据类型(简单类型,内置类型): 字节型(byte),短整型(short),整型(int),长整型(long),字符型(char),浮点型(float),双精度型(do ...

  9. **python实现的单例模式

    设计模式中,最简单的一个就是 “单例模式”. 所谓单例,是指一个类只有一个全局实例. 单例模式的使用场景: 1. Windows的Task Manager(任务管理器)就是很典型的单例模式(这个很熟悉 ...

  10. SignalR web实时同步 消息推送 广播

    源码:https://github.com/SignalR/SignalR demo:http://download.csdn.net/download/qq_21533697/9702791#com ...