实际应用过程中,还是有很多的需求需要在Grid中呈现较多的数据量,这就涉及到UltraGrid的大数据量绑定的支持。
好在UltraGrid考虑了这点,下面就介绍如何在最优性能下将大数据量绑定到UltraGrid上。
1. 拖放Grid到Form,增加一些Column,OK后选择第一个选项(产生DataSource)
如下将Grid的数据源关联到UltraDataSource上

2. 定义全局List,存放数据源

3. Load Data的事件中写入如下Code
几个关键步骤需要说明:
SetCount – 告诉UltraDataSource以供需要显示多少数据
LoadStyle – 告诉UltraGrid以虚拟模式的方式加载数据
4. 手动绑定UltraDataSource和List(如同上一贴讲的一样)
记得CacheData一定要设置为False,否则性能得不到明显的提高
5. 虚拟模式是告诉Grid只加载当前一屏所能看到的Row的数据,而每当拖动滚动条的时候,Grid都会重新加载可见行的数据,相当于将一次绑定的消耗分散到
每一次操作上,用户体验有所提高;
但,客户端需要首先获取所有的数据到List中,这点似乎是不可避免的,尤其是采用了三层架构之后,所以可不考虑;
另外,由于Grid当前只有一屏的数据呈现,所以如果要提供Sort等操作,则必须考虑要自己重写Sort事件,
因为Grid的Sort会将所有的数据进行Load然后排序,这样子又会造成性能低下的效果,所以建议取消Sort事件。

Begin for Huge Data Bind to Grid#region Begin for Huge Data Bind to Grid
private List<User> m_HugeDateList = new List<User>();
private void uButtonBindHugeData_Click(object sender, EventArgs e)
...{
m_HugeDateList = DataHelper.GetHugeUserList();
this.uGridHugeData.BeginUpdate();
// Set the row count. This is how many rows we will have.
this.uDataSourceHuge.Rows.SetCount(m_HugeDateList.Count);
// Set the LoadStyle to LoadOnDemand so the UltraGrid doesn't pre-load
// all the rows. LoadOnDemand load style creates rows as they are needed
// (for example when they are scrolled into view). You must do this
// before setting the DataSource on the UltraGrid.
this.uGridHugeData.DisplayLayout.LoadStyle = LoadStyle.LoadOnDemand;
// Set the datasource to the grid
this.uGridHugeData.DataSource = this.uDataSourceHuge;
this.uGridHugeData.DataBind();
this.uGridHugeData.EndUpdate();
}
private void uDataSourceHuge_CellDataRequested(object sender, CellDataRequestedEventArgs e)
...{
User user = m_HugeDateList[e.Row.Index];
switch (e.Column.Key)
...{
case "ID":
e.Data = e.Row.Index + 1;
break;
case "Name":
e.Data = user.Name;
break;
case "Sex":
e.Data = user.Sex == Sexes.Girl ? Properties.Resources.FeMale : Properties.Resources.Male;
break;
case "Age":
e.Data = user.Age;
break;
case "Team":
e.Data = user.Team;
break;
default:
break;
}
// Don't cache the previewed data in memory, so load fresh data everytime
e.CacheData = false;
}
private void uGridHugeData_InitializeLayout(object sender, InitializeLayoutEventArgs e)
...{
// ScrollBounds of ScrollToFill will prevent the user from scrolling the
// grid further down once the last row becomes fully visible.
e.Layout.ScrollBounds = ScrollBounds.ScrollToFill;
// Set the scroll style to immediate so the rows get scrolled immediately
// when the vertical scrollbar thumb is dragged.
e.Layout.ScrollStyle = ScrollStyle.Immediate;
// Disabled the Sort Event
e.Layout.Override.HeaderClickAction = HeaderClickAction.Select;
}
#endregion

本文介绍如何使用UltraGrid高效地绑定大量数据,并通过虚拟模式提高性能。包括设置数据源、调整加载方式及禁用缓存等步骤。

851

被折叠的 条评论
为什么被折叠?



