

一、数据装载完毕后,修改dataGridView的列名【如何修改?自行搜索】——此法较笨
二、dataGridView数据绑定前,修改数据源(DataTable)的列名,如何修改,在Select sql的时候修改
SELECT id,
username,
nickname,
password,
sex,
email,
phone,
remark
FROM user;


其它数据库里面有一个函数row_number(),sqlite里面没有,所以只能曲线救国
select id, (select count(*) from user b where a.id >= b.id) as cnt from user a
var table = DB.GetUserTable(); //读表
var pageInfo = GetSpecifiedPage(1,30,ref table); //获取第一页的数据
bindingSource.DataSource = pageInfo.table; //设置数据源
dataGridView1.DataSource = bindingSource;
数据流示意图


| 页码 (第x页) | 行号起始值 | 行号结束值 |
|---|---|---|
| 1 | 0 | 29 |
| 2 | 30 | 59 |
| 3 | 60 | 89 |
| 4 | 90 | 90 |
比如现在要显示第2页的数据,那么直接把第30到第59行的数据重新装到一个新表里,然后把这个新表设置成dataGridView的数据源就行。
用Linq,Skip属于前面页码的rows,然后Take30行即可
///
/// 给定一个DataTable,按每页指定行数分页后,取某一页的所有数据
/// 实现:用List进行切片[Skip Take]
///
/// 要取数据的页号(1...n)
/// 每页包含多少行
/// 要处理的table
/// 新的DataTable
public static (bool success,DataTable table,string info) GetSpecifiedPage(int pageIndex,int rowsNumInOnePage,ref DataTable table)
{
(bool success, DataTable table, string info) rtn ;
//**总页数计算
float t = (float)table.Rows.Count / (float)rowsNumInOnePage;
var totalPages = (int)(Math.Ceiling(t));
//**分页取数据
if(pageIndex < 1 || pageIndex > totalPages) //页码非法
{
rtn = (false, new DataTable(), "给定的页码越界");
}
else //页码合法,提取数据
{
//切片处理进行分页
var skipRows = (pageIndex - 1) * rowsNumInOnePage; //跳过前面页面的DataRow
var takeRows = rowsNumInOnePage; //取一整页的DataRow
var rows = table.AsEnumerable().Skip(skipRows).Take(takeRows).ToList();
var rtnTable = new DataTable();
//rtnTable = table.Copy(); //DataTable.Copy() returns a DataTable with the structure and data of the DataTable.
//rtnTable.Clear();
rtnTable = table.Clone(); //Unlike Copy(), DataTable.Clone() only returns the structure of the DataTable, not the rows or data of the DataTable.
rows.ForEach(r => rtnTable.ImportRow(r));
rtn = (true,rtnTable,"");
}
return rtn;
}
var rtnTable = new DataTable();
rtnTable = table.Copy(); //DataTable.Copy() returns a DataTable with the structure and data of the DataTable.
rtnTable.Clear();
rows.ForEach(r => rtnTable.ImportRow(r));
var rtnTable = new DataTable();
rtnTable = table.Clone(); //Unlike Copy(), DataTable.Clone() only returns the structure of the DataTable, not the rows or data of the DataTable.
rows.ForEach(r => rtnTable.ImportRow(r));