用大量数据填充 DataGridView 的最佳方法 [英] Best way to fill DataGridView with large amount of data

查看:34
本文介绍了用大量数据填充 DataGridView 的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体,它有两个 DataGridViews (DGV),每个窗体将保存 25,000 多条记录和 21 列.我已经使用 DataAdapter 成功地从数据库中加载了每个数据,然后我尝试使用 for 循环简单地填充 DGV.每种方法花费的时间大致相同.第一次将数据填充到 DGV 时花费的时间太长(7 分钟以上),随后的时间更加合理(约 30 秒).所以我的问题是,用平均 <= 1 分钟的大量数据加载 DGV 的最佳方法是什么?我真的很喜欢 DGV 的功能,但如果迫不得已,我愿意使用不同的技术,即使这意味着放弃某些功能.

I have a windows form that has two DataGridViews (DGVs) that will hold 25,000+ records and 21 columns each. I have successfully loaded each with the data from the DB using a DataAdapter and then I tried simply filling the DGVs using for loops. Each method took roughly the same amount of time. The first time the data is filled into the DGVs it takes too long (7+ mins), and then the subsequent times the time is much more reasonable (~30 secs). So my question is, what is the best way to load a DGV with a large amount of data that will take on average <= 1 min? I really like the functionality of DGVs, but if push comes to shove I am willing to use a different technology, even if it means giving up some of that functionality.

推荐答案

DataGridView

  • 在循环中手动创建行,正如您目前所做的:正如您所注意到的,如果您有大量数据,效率会非常低

  • Create the rows manually in a loop, as you are currently doing: as you have noticed, it's very inefficient if you have a lot of data

使用 DataGridView 的虚拟模式,正如乔纳森在他的评论中所建议的:DGV 只创建尽可能多的行,并在用户滚动时动态更改其内容.您需要处理 CellValueNeeded 事件以向 DGV 提供所需的数据

Use the DataGridView's virtual mode, as suggested by Jonathan in his comment: the DGV only creates as many rows as can be displayed, and dynamically changes their contents when the user scrolls. You need to handle the CellValueNeeded event to provide the required data to the DGV

使用数据绑定:这是迄今为止最简单的方法.您只需使用 DbDataAdapter 用数据库中的数据填充 DataTable,然后将此 DataTable 分配给 DGV 的 DataSource属性.DGV 可以自动创建列(AutoGenerateColumns = true),也可以手动创建(必须将列的 DataPropertyName 设置为您想要的字段名称显示).在数据绑定模式下,DGV 的工作方式类似于虚拟模式,只是它负责从数据源中获取数据,因此您无需做任何事情.即使对于大量行,它也非常有效

Use databinding: that's by far the easiest way. You just fill a DataTable with the data from the database using a DbDataAdapter, and you assign this DataTable to the DGV's DataSource property. The DGV can automatically create the columns (AutoGenerateColumns = true), or you can create them manually (you must set the DataPropertyName of the column to the name of the field you want to display). In databound mode, the DGV works like in virtual mode except that it takes care of fetching the data from the datasource, so you don't have anything to do. It's very efficient even for a large number of rows

这篇关于用大量数据填充 DataGridView 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆