[英]How to speed up WPF DataGrid?
The background: I have arbitrary query that is executed and then I fetch data -- array of records = arrays of objects. 背景:我执行了任意查询,然后获取数据-记录数组=对象数组。 I would like to view them using DataGrid.
我想使用DataGrid查看它们。
The problem: It does not feel that app is responsive enough. 问题:感觉应用程序响应不够快。 Switching from query to query is slow, it takes around 1-2 seconds to switch, and there are only ~20 records to display!
从查询切换到查询的速度很慢,切换大约需要1-2秒,并且只能显示约20条记录! I want to make the switch instantly, you click on "next query" and you get the results right away.
我要立即进行切换,您单击“下一个查询”即可立即获得结果。
The code: 编码:
// defining colums for a grid
grid.Columns.Clear();
int i = 0;
foreach (var db_col in query.Names) // names of the colums
{
var col = new DataGridTextColumn();
col.Header = db_col;
col.Binding = new Binding(String.Format("Data[{0}]",i));
grid.Columns.Add(col);
++i;
}
// adding rows to grid -- the culprit
grid.Items.Clear();
foreach (var db_row in query.Rows)
{
var row = new DataGridRow();
row.Item = db_row;
grid.Items.Add(row);
}
I cannot see what I can other way -- I iterate over rows and I add them one by one. 我看不到我能以其他方式看到的东西-我遍历行并将它们一一添加。 Rows are already in the memory, there is no database communication at this point.
行已经在内存中,此时没有数据库通信。 How do I know this (adding rows) is a reason for slowdown?
我怎么知道(添加行)是速度下降的原因? Quite straightforward, I comment this out and the app becomes fast.
非常简单,我将其注释掉,该应用变得很快。
My box: Windows 7 Ultimate 32-bit, CPU Intel Core2Duo 2.66GHz, 2GB RAM. 我的盒子:Windows 7 Ultimate 32位,CPU Intel Core2Duo 2.66GHz,2GB RAM。
Have you tried calling BeginInit and EndInit to stop the grid getting updated after each item is added? 您是否尝试过调用BeginInit和EndInit来停止在添加每个项目后更新网格?
grid.BeginInit();
grid.Items.Clear();
foreach (var db_row in query.Rows)
{
var row = new DataGridRow();
row.Item = db_row;
grid.Items.Add(row);
}
grid.EndInit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.