WPF 数据网格性能
我有一个非常大的逗号分隔文本文件。我需要在 WPF 数据网格中显示它,哪种方法可以实现将所有数据加载到网格的最高性能?我只知道两种方法:
- 使用数据表,并将每一行添加为行(看起来像过度杀伤)
- 使用 ObservableCollection,为每行创建对象(看起来像过度杀伤)
是否有第三种方法来填充数据网格,这将提供更高的性能?
I have a very large comma-seperated text-file. I need to display this in a WPF Datagrid, which method would result in the highest performance of loading all the data to the grid? I'm only aware of two methods:
- Using a Datatable, and adding each line as row (looks like overkill)
- Using an ObservableCollection, creates object for each line (looks like overkill)
Is there a third method to fill the Datagrid, which would give higher performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于您打算如何处理数据。如果数据是只读的,您可以定义一个列表并将其设置为数据网格的 ItemsSource。然后数据网格将自动创建所有行来表示列表中的项目。
如果要操作数据,您可以使用 BindingList<>或 ObservableCollection<>。
“为每行创建对象”怎么会是矫枉过正呢?您必须将一行/一行数据表示为一个对象。这只是常识,不可能以任何其他方式做到这一点。
基本上,如果您追求的是性能,那么数据网格的 ItemsSource 必须尽可能轻量。如果您不打算使用 DataTable 的所有功能,则 DataTable 可能有点大材小用。列表<>另一方面是重量尽可能轻。
另请注意,WPF 数据网格默认使用虚拟化,这也有助于提高性能。有一些警告,例如不要将数据网格放在堆栈面板中,否则虚拟化效果会消失 - 您可以通过谷歌搜索这一点。
最后一件事,根据我的经验,.NET4 WPF 内置数据网格有一个严重的设计错误。基本上,即使对于最简单的数据,每个 DataGridRow 也会消耗 1MB 的内存。通过虚拟化并且仅显示 30 行,这并不是什么大问题 - 仅消耗 30MB 内存。但是拿一千行,关闭虚拟化,内存消耗是3GB!!!!相比之下,对于相同的数据,WinForm datagridview 消耗的内存要少 10 倍以上。即使禁用虚拟化,一千行也只需要 30MB...
我已经在 Connect 中打开了一个关于此的错误,但专业专家尚未研究此问题。不确定 WPF 工具包数据网格是否表现得更好......
Depends on what you intend to do with the data. If the data is going to be read only, you can define a List and set it as the ItemsSource for the datagrid. Then the datagrid will automatically create all the rows to represent items in the list.
If the data is to be manipulated you can use a BindingList<> or an ObservableCollection<>.
How is "creates object for each line" an overkill? You have to represent a line/row of data as an object. That's just common sense, it's not possible to do it any other way.
Basically, if performance is what you are after then the datagrid's ItemsSource has to be as light weight as possible. A DataTable might be an overkill, if you don't intend to use all of it's features. A List<> on the other hand is as light weight as possible.
Also note that the WPF datagrid by default uses virtualization, which helps performance as well. There are some caveats thow, like don't put the datagrid inside a stackpanel otherwise the virtualization effect goes away - you can google about this.
One final thing, by my experience the .NET4 WPF builtin datagrid has a serious design bug. Basically each DataGridRow consumes 1MB of memory even for the simplest of data. With virtualization and only showing 30 rows, it's not a huge deal braker - only 30MB of memory consumed. But take a thousand rows, turn off virtualization and the memory consumption is 3GB!!!! On contrast, a WinForm datagridview, consumes more than 10x less memory with the same data. Even with virtualization disabled a thousand rows only takes 30MB...
I've opened a bug about this in Connect, but the specialized experts have yet to look into this issue. Not sure if the WPF toolkit datagrid behaves any better...
如果使用 OleDb 加载逗号分隔的文件,则可以绑定到 DataView。
注意:我没有测试大型数据集。
这是到 DataGrid 的绑定:
这是 ViewModel:
这是我使用的数据文件:
You can bind to a DataView if you load a comma delimited file using OleDb.
Note: I did not test a large dataset.
Here is the binding to the DataGrid:
Here is the ViewModel:
Here is the data file i used: