【发布时间】:2009-05-09 00:15:17
【问题描述】:
我正在使用 System.Windows.Forms.DataGrid。它填充了大约 3000 行,并且重绘非常缓慢。如果我最小化和最大化我的表单,所有其他控件只会显示,但我最终会看到 DataGrid 逐行重绘。此 DataGrid 中的所有内容都是只读的(如果有影响)。
更新:
我不确定如何为我的项目正确实施 CellValueNeeded() 事件,或者它是否有助于提高我的 DataGrid 的性能。
我正在创建一个包含 DataGridView 的用户控件(请参见下面的代码)。当调用 SetProject() 方法时,我的控件设置为我的 Project 类的特定实例。然后控件使用静态方法 Informa.Data.GetProjectDataTable(Project proj) 从 Project 中提取 DataTable。然后将 DataGrid 的 DataSource 属性设置为返回的 DataTable。
这是我第一次使用 ADO 或 DataGrids 做任何事情,所以请耐心等待。看起来 CellValueNeed() 允许我覆盖 DataGrid 如何为其中一个单元格查找值,但在我的情况下,这比 MSDN 上的示例复杂得多。我的数据的实际来源是各种 Node 对象的树形结构,其根是 Project 实例。每个节点都可以有一组可变的属性,用户也可以在运行时对其进行扩展。然后还有许多其他复杂性,节点从其父节点继承属性值,并从其子节点汇总其他属性。
Informa.Data.GetProjectDataTable() 消除了所有这些疯狂并生成所有节点的所有属性的单个平面数据表。在这一点上,我不关心能够将此表的任何更改与原始树结构相关联,或者在树结构更改时更新表的特定部分。我要做的就是在 DataGrid 中向用户显示数据。
那么我是否实现 CellValueNeeded() 以从项目提供的 DataTable 中读取?我认为 DataGrid 已经知道如何高效地将 DataTable 用作 DataSource?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Informa;
namespace Informa
{
public partial class ProjectGridControl : UserControl
{
private Project proj;
public ProjectGridControl()
{
InitializeComponent();
}
public void SetProject(Project proj)
{
this.proj = proj;
UpdateGridControl();
}
public void UpdateGridControl()
{
if (this.proj == null)
{
this.dataGrid.DataSource = null;
}
else
{
//Extracts a DataTable from the project and sets it as the
//DataSource of the property grid
this.dataGrid.DataSource = Informa.Data.GetProjectDataTable(proj);
}
}
}
}
【问题讨论】:
-
建议您发布一些(缩减的)代码...
-
触发了什么事件?你在处理什么事件?如果您在绑定的每个列/行/单元格上运行代码,我会在那里检查。
-
我没有处理任何事件。我正在创建一个 DataTable 并将其设置为 DataGrid 的 DataSource。就是这样。