private int pageIndex = 0;//分页索引号
private int pageSize = 10000;//分页大小
private int scrollRowIndex = 0;//滚动条滚动到第几行
private int count = 0;//数据总条数
List<TestSelectModel> userList = new List<TestSelectModel>();//数据集合
/// <summary>
/// 刷新初始化数据
/// </summary>
private void Init()
{
pageIndex = 0;
this.dgv.DataSource = null;
userList = null;
scrollRowIndex = 0;
count = 0;
loadPage();
}
//加载翻页数据
private void loadPage()
{
//判断当总数据条数是否等于当前已经获取的数据总条数的时候不需要再加载了
if (count > 0 && count == userList.Count)
{
return;
}
int countIndex = pageIndex > 0 ? pageIndex * pageSize : 0;
F202_PageModel<TestSelectModel> pageModel = tSBLL.GetPage(countIndex, pageSize);
count = pageModel.TotalCount;
List<TestSelectModel> newList = pageModel.ReList;
userList = userList != null && userList.Count > 0 ? userList.Union(newList).ToList() : newList;
var query = from i in userList
orderby i.Id
select i;
DataTable table = query.CopyToDataTable();
BindingSource source = new BindingSource();
source.DataSource = table;
this.dgv.AutoGenerateColumns = false;
this.dgv.DataSource = source;
dgv.FirstDisplayedScrollingRowIndex = scrollRowIndex;
}
/// <summary>
/// 表格滚动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgv_Scroll(object sender, ScrollEventArgs e)
{
if (isCache)
return;
if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
{
scrollRowIndex = e.NewValue + dgv.DisplayedRowCount(false);
if (e.NewValue + dgv.DisplayedRowCount(false) > dgv.Rows.Count - 5)
{
//这里面写加载数据的相关操作逻辑
++pageIndex;
loadPage();
}
}
}
Win form中DataGridView表格滚动加载数据
于 2023-09-28 14:58:46 首次发布