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 + "\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数据分页的更多相关文章
- [.NET] SQL数据分页查询
[.NET] SQL数据分页查询 程序下载 范例下载:点此下载 原始码下载:点此下载 NuGet封装:点此下载 数据查询 开发系统时,使用C#执行SQL查询指令,就可以从SQL数据库里查询所需数据. ...
- C#针对DataTable进行分页方法
以下的分页方法是针对数据量不是非常大的数据进行的,是在内存中进行的分页操作. /// <summary> /// DataTable分页 /// </summary> /// ...
- js 从一个json拼接成另一个json,并做json数据分页table展示
先给数据: //原始json数据json = [{"id":"1","aid":"013","performa ...
- thinkphp5配合datatable插件分页后端处理程序
thinkphp5配合datatable插件分页后端处理程序第一版DataTable.php v.1.0 <?php use think\Db; /** * DataTable.php. */ ...
- ASP.NET真分页_接前篇引用AspNetPager.dll进行数据分页
一.前端准备工作 1.之前我写到过<Asp.net中引用AspNetPager.dll进行数据分页> 这种分页方式只能在前台将数据分页,而每次点击查询时对目标数据库还是全查询,这样不仅会 ...
- mysq大数据分页
mysql limit大数据量分页优化方法 Mysql的优化是非常重要的.其他最常用也最需要优化的就是limit.Mysql的limit给分页带来了极大的方便,但数据量一大的时候,limit的性能就急 ...
- excel to datatable (c#用NPOI将excel文件内容读取到datatable数据表中)
将excel文件内容读取到datatable数据表中,支持97-2003和2007两种版本的excel 1.第一种是根据excel文件路径读取excel并返回datatable /// <sum ...
- DataTable数据批量写入数据库三种方法比较
DataTable数据批量写入数据库三种方法比较 标签: it 分类: C#1) insert循环插入:2) sqldataadapter.update(dataset,tablename); ...
- c# applibrary实现一个Sheet表中存放多张DataTable数据
1.工具类(applibrary.dll) public class ExcelHelper { /// <summary> /// 文件名 /// </summary> pu ...
随机推荐
- 学习FPGA过程中的理论知识
学习FPGA,先要有数电知识,最好有点C语言,,学好硬件描述语言,verilog或者vhdl.在有这些基础上,做一些小的模块不断积累.这里不再赘述. 下面介绍一下关于FPGA学习过程中的一些理论知识. ...
- Java堆外内存之一:堆外内存场景介绍(对象池VS堆外内存)
最近经常有人问我在Java中使用堆外(off heap)内存的好处与用途何在.我想其他面临几样选择的人应该也会对这个答案感兴趣吧. 堆外内存其实并无特别之处.线程栈,应用程序代码,NIO缓存用的都是堆 ...
- kotlin学习二:初步认识kotlin
1. 函数 kotlin中支持顶级函数(文件内直接定义函数),对比JAVA来说,JAVA的程序入口是main方法,kotlin也一样,入口为main函数 首先看下kotlin中main函数的定义. f ...
- Warning: require(D:\wamp\www\glink-smart\bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in D:\wamp\www\glink-smart\bootstrap\autoload.php on line 1
Laravel访问出错错误信息:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or dire ...
- oozie工作流相关入门整理
Oozie支持工作流,其定义通过将多个Hadoop Job的定义按照一定的顺序组织起来,然后作为一个整体按照既定的路径运行.一个工作流已经定义了,通过启动该工作流Job,就会执行该工作流中包含 ...
- node+express+socket.io制作一个聊天室功能
首先是下载包: npm install express npm install socket.io 建立文件: 服务器端代码:server.js var http=require("http ...
- canvas之画一个三角形
<canvas id="canvas" width="500" height="500" style="background ...
- 什么是 MVC ?
本篇博客打算简单介绍一下MVC是什么,为接下来MVC的学习做一下铺垫. MVC是一种架构设计模式,是一种设计理念.是为了达到分层设计的目的,从而使代码解耦,便于维护和代码的复用.MVC是3个单词的缩写 ...
- RefWorks
RefWorks公司简介/RefWorks 编辑 RefWorks是美国剑桥信息集团的子公司,是ProQuest 的姊妹公司.该公司于2001年由参考文献管理领域的一些专家组建而成,并致力于为学术机构 ...
- cookies,sessionStorage,localStorage的区别
sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...