背景工作者的问题。 [英] Problem regarding background worker.

查看:126
本文介绍了背景工作者的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个20000行的访问数据库,我希望使用backgroundworker在datagridview中显示所有行。在datagridview中加载数据后一段时间它被绞死所以我甚至看不到所有数据都被加载了datagridview.please帮助,如果有其他方法使用backgroundworker..plz帮助将数据从数据库加载到datagridview ...我在这里发布我的代码..



I have an access database with 20000 rows and i want to display all the rows in a datagridview using backgroundworker.while loading data in the datagridview after certain time its getting hanged so i cant even see that what all data have been loaded in the datagridview.please help if there is some other way to load data from database to the datagridview using backgroundworker..plz help...i am posting my code here..

Private Sub frmBgData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Control.CheckForIllegalCrossThreadCalls = False
      bgwWork.RunWorkerAsync()
  End Sub
  Private Sub loadGrid()
      dgvBack.Rows.Clear()
      Try
          If con.State Then con.Close()
          con.Open()
          sqlselect.Connection = con
          sqlselect.CommandText = "select Order_Details,Order_No from Ordertable"
          adpt.SelectCommand = sqlselect
          'readr = sqlselect.ExecuteReader

          i = 0
          adpt.Fill(dts, "grid")

          While i <= dts.Tables("grid").Rows.Count - 1

              dgvBack.Rows.Add(dts.Tables("grid").Rows(i).Item("Order_Details").ToString, dts.Tables("grid").Rows(i).Item("Order_No").ToString)
              i += 1
          End While



      Catch ex As Exception
      End Try
  End Sub

  Private Sub bgwWork_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwWork.DoWork
      loadGrid()
  End Sub

  Private Sub bgwWork_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwWork.RunWorkerCompleted
      MsgBox("done")
      dts.Tables.Clear()
      dts.Dispose()
      adpt.Dispose()
      con.Close()

  End Sub

推荐答案

问题#1:Control.CheckForIllegalCrossThreadCalls = False



绝不能永远不要这样做!立即从代码中删除此行。



问题#2:不要向DataGridView添加20,000行。用户可以查看所有20,000行吗?不,没有人想要滚动浏览它们。如果您需要显示多个hundres行,Google需要DataGridView虚拟模式并开始阅读。



从数据库中检索20,000行并不是'只要尝试将DGV绑定到它们,就要靠近它们。您不能将该操作放在后台线程上,因此您的应用程序将在发生绑定时挂起。关于这一点你没有什么可以做的,除了不要把2万行送到DATAGRIDVIEW!这就是虚拟模式的用途。
Problem #1: "Control.CheckForIllegalCrossThreadCalls = False"

NEVER NEVER NEVER DO THIS!! Remove this line from your code right now.

Problem #2: Do NOT add 20,000 rows to a DataGridView. Can a user SEE all 20,000 rows? No and nobody want to go scrolling through them all. If you have the need to show more than a couple of hundres rows, Google for "DataGridView Virtual Mode" and start reading.

Retrieving 20,000 rows from the database doesn't take near as long as trying to bind the DGV to them. You can't put that operation on a background thread, so your app is going to be hung while that bind is taking place. There's nothing you can do about that, other than DON'T STUFF 20,000 ROWS INTO A DATAGRIDVIEW! That's what Virtual Mode is for.


这篇关于背景工作者的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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