标签:
http://blog.csdn.net/educast/article/details/4769457
evExpress是一套第三方控件
其中有类似DataGridView的控件
今天把针对DevExpress.XtraGrid.GridControl实时加载数据的功能开发出来了
分享给大家
欢迎讨论
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
namespace Life365.CallCenter
{
public class GridCreator<T> where T: new()
{
//一页有多少条数据
private int page_size = 30;
public int PageSize
{
get { return page_size; }
}
/// <summary>
/// 过滤条件
/// </summary>
public string Filter
{
get;
set;
}
/// <summary>
/// 排序条件
/// </summary>
public string OrderBy
{
get;
set;
}
/// <summary>
/// 数据访问类
/// </summary>
public BaseProxy Proxy
{
get;
set;
}
private int row_count;
/// <summary>
/// 总行数
/// </summary>
public int RowCount
{
get {return row_count;}
}
private int page_num = 1;
/// <summary>
/// 当前的数据页
/// </summary>
public int PageNum
{
get { return page_num; }
}
//缓存的数据
private List<T> data_list = new List<T>();
private GridCreator()
{
}
/// <summary>
/// 构造函数
/// </summary>
public GridCreator(GridControl grid,BaseProxy proxy)
{
((GridView)grid.MainView).TopRowChanged += new EventHandler(grid_TopRowChanged);
this.Proxy = proxy;
grid.DataSource = data_list;
}
/// <summary>
/// 获取前两页数据
/// </summary>
private void GetTwoPageData()
{
data_list.InsertRange(0, GetData());
page_num++;
data_list.InsertRange(page_size, GetData());
}
/// <summary>
/// grid下拉事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void grid_TopRowChanged(object sender, EventArgs e)
{
GridView grid = sender as GridView;
if (grid.TopRowIndex >= (page_num - 1) * page_size)
{
//当看完一页数据的时候开始取数据
page_num++;
List<T> test = GetData();
data_list.InsertRange((page_num - 1)* page_size, test);
}
}
/// <summary>
/// 实时的加载数据函数
/// </summary>
/// <returns></returns>
public List<T> GetData()
{
return Proxy.GetDataList(this.Filter, this.OrderBy, this.page_size,this.page_num, ref this.row_count) as List<T>;
}
}
}
T泛型约束是需要加载数据的类型
BaseProxy是访问数据库的基类,其中包含GetDataList的虚方法
继承自BaseProxy的GetDataList为
Code
public override object GetDataList(string filter, string order_by, int page_size, int page_num, ref int count)
{
count = 1000;
List<RegionEntity> regions = new List<RegionEntity>();
for (int i = 0; i < page_size; i++)
{
regions.Add(new RegionEntity() { SR_RegionName = ((page_num-1)*page_size+i).ToString() });
}
return regions;
}
这里的RegionEntity就是前面提到的某一种类型的T
数据提供程序是模拟出来的(测试驱动开发,哈哈)
你可以在这个函数里写数据库访问方法
具体调用方法如下
Code
GridCreator<RegionEntity> creator = new GridCreator<RegionEntity>(gridShop,new UtilsProxy());
UtilsProxy类继承自BaseProxy并重写了GetDataList方法
gridShop就是当前窗体的DevExpress.XtraGrid.GridControl控件
RegionEntity针对泛型类型
用起来还是蛮方便的
DevExpress的GridControl的实时加载数据解决方案(取代分页)
标签:
原文地址:http://www.cnblogs.com/yibinboy/p/5572503.html