[WinForm每日一帖] UltraGrid大数据量的绑定
2007-06-12 11:13
549 查看
实际应用过程中,还是有很多的需求需要在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考虑了这点,下面就介绍如何在最优性能下将大数据量绑定到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
相关文章推荐
- [WinForm每日一帖] 绑定父子关系的数据到UltraGrid
- [WinForm每日一帖] UltraGrid中实现下拉Grid(UltraDropDown)
- [WinForm每日一帖] UltraGrid导出数据到Excel
- [WinForm每日一帖] UltraGrid指定栏位实现Summary
- [WinForm每日一帖] 如何判断UltraGrid中是否选中数据
- [WinForm每日一帖] UltraGrid如何自定义Row Filter的值
- [WinForm每日一帖] UltraGrid中实现下拉列表列
- [WinForm每日一帖] UltraGrid中增删改功能推荐做法
- [WinForm每日一帖] UltraGrid导出数据到Excel
- [WinForm每日一帖]TableLayoutPanel的两个小技巧
- [WinForm每日一帖] 如何使用UltraToolTipManager为控件提供带样式的ToolTip
- [WinForm每日一贴] 如何实现UltraGrid中的图片按钮效果
- [WinForm每日一帖] 如何验证Editor中输入的数据合法性
- [WinForm每日一帖] 如何选中下拉框中指定的数据
- [WinForm每日一帖] 如何使你的窗体样式固定,不因系统设定而变化
- [WinForm每日一帖] 如何使用资源文件提供Excel模板下载
- [WinForm每日一帖] UltraComboEditor和UltraCombo的介绍
- [WinForm每日一帖] 如何制作复杂表头的Grid
- [WinForm每日一帖] UltraGrid打印预览
- [WinForm每日一帖] 如何使用UltraCurrencyEditor