将数据表加载到 DataGridview 时如何显示进度条? [英] How do i display a progress bar while loading a datatable to DataGridview?

查看:70
本文介绍了将数据表加载到 DataGridview 时如何显示进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 GetAllRecords() 的方法,它将返回一个包含 20 万条记录的数据表.当我检查时,将数据加载到数据网格大约需要 5 秒钟,并且窗口冻结了.

I have a method called GetAllRecords() that will return a datatable of 200K records. When I checked it took about 5 seconds to load data to the datagrid and window froze.

DataTable dt = Records.GetAllRecords();

我知道我必须使用后台工作控件并且它是三个事件,但我不知道如何正确使用它们.例如,进度条必须显示真实进度而不是虚假进度.我可以使用线程类的 sleep() 减慢进度条,但这将是一个假进度条.

I know I have to use Background worker controls and it's three events, but I cannot figureout how to use them properly. For instance, the progress bar must show real progress not a fake one. I could slow the progress bar using sleep() of the thread class but it would be a fake progress bar.

  1. 这里有人能告诉我如何在我的窗体上创建一个真正的进度条的分步说明吗?

  1. Could any one here please give me step by step instructions on how to create a real progress bar on my windows form?

.NET 2.0之前没有Background worker控件,那时候程序员是怎么写真正的进度条的?

Before .NET 2.0 there was not Background worker control, in those days how programmer wrote real progress bars?

谢谢

推荐答案

如果您搜索该站点以及 Google,您会发现有多种尝试这样做:

If you do a search of the site, as well as Google, you'll see that there are several attempts at doing this:

SO 链接

MSDN 链接

这些解决方案基本上会在您将每一行添加到网格后更新进度条,但如果您尝试显示实际数据库调用的进度,我认为您无法非常准确.

These solutions basically update the progress bar after you add each row to the grid, though if you're trying to show progress for an actual database call I don't think you'll be able to be very accurate on that.

我认为您将遇到的主要问题是您将没有事件挂钩来让您确切知道绑定的耗时部分在哪里发挥作用.我假设 将数据绑定到网格需要 5 秒,并且您没有计算实际拉取记录的加载时间.

I think the main problem you'll have is that you won't have event hooks to let you know exactly where the time-consuming part of the binding comes into play. I'm assuming that it takes 5 seconds once you bind the data to the grid and that you're not counting the load time of actually pulling down the records.

Pre-BackgroundWorker

BackgroundWorker 类基本上只是为了方便您自己处理线程并为您调用返回到 UI 线程的任何进度.您必须生成自己的线程,并在将数据应用到 UI 线程的工作完成后在您的控件上调用 BeginInvoke,因为您不能在任何线程上对 UI 进行更改,但主要的.

The BackgroundWorker class is basically just a convenience to you handling the threading yourself and invoking any progress back to the UI thread for you. You would have to spawn your own thread, and call BeginInvoke on your control once work is done to apply the data to the UI thread, since you can't make changes to the UI on any thread but the main one.

这篇关于将数据表加载到 DataGridview 时如何显示进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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