2013-09-30 44 views
2

这是我用于填充的DataGridView代码:如何填充更快的datagridview在vb.net

Public Sub generate_list_ftMK(ByVal clbBranch As CheckedListBox, ByVal dtpSDate As DateTimePicker, ByVal dtpEDate As DateTimePicker, ByVal dvList As DataGridView) 
    Dim ds As DataSet = New DataSet() 
    Dim da As MySqlDataAdapter 


    Dim strQ As String = String.Empty 
    Dim strQ1 As String = String.Empty 


    Dim colItemCode As String = String.Empty 
    Dim colReg_Pri As String = String.Empty 
    Dim colXL_Pri As String = String.Empty 
    Dim colDel_Date As String = String.Empty 
    Dim colLabel As String = String.Empty 
    Dim colDays As String = String.Empty 
    Dim PCT As String = String.Empty 

    Try 
     If con.State = ConnectionState.Open Then con.Close() 
     con.Open() 

     'clear the dataset 
     ds.Tables.Clear() 

     'loop for all branches 
     For i = 0 To clbBranch.CheckedItems.Count - 1 

      Application.DoEvents() 

      'get the Branch Code 
      chkBranch = clbBranch.CheckedItems(i).ToString 

      'select item code and price where JO_TYPE is JO 
      strQ = "select " & _ 
        "CONCAT(a.STYLE_CODE, '-', LPAD((a.JO_NO), 4, '0'), '-', d.LINE_NO) as Item_Code, " & _ 
         "DATE_FORMAT(c.DEL_DATE, '%Y-%m-%d') as DEL_DATE, " & _ 
         "d.REG_PRI as Reg_Price, " & _ 
         "d.XL_PRI as XL_Price, " & _ 
         "a.LABEL_CODE, " & _ 
         "a.STYLE_CODE, " & _ 
         "LPAD((a.JO_NO), 4, '0')," & _ 
         "d.LINE_NO, " & _ 
         "DATEDIFF('" & Format(Date.Now, "yyyy-MM-dd") & "',DATE_FORMAT(c.DEL_DATE, '%Y-%m-%d')) as Days " & _ 
        "from " & _ 
         "jo_hdr as a " & _ 
          "inner join " & _ 
         "branch as b ON a.BRNCH_CODE = b.BRNCH_CODE " & _ 
          "inner join " & _ 
         "dr_hdr as c ON c.TRAN_REF = a.TRAN_NO " & _ 
          "inner join " & _ 
         "dr_det as d ON d.DR_NO = c.DR_NO " & _ 
        "where " & _ 
         "c.DEL_DATE BETWEEN '" & dtpSDate.Text & "' AND '" & dtpEDate.Text & "' " & _ 
          "and a.LABEL_CODE = '" & FtMK_Code & "' " & _ 
          "and a.BRNCH_CODE = '" & chkBranch & "' " & _ 
          "and SUBSTRING(d.REG_PRI,LENGTH(d.REG_PRI) - 1,2) = 75 " & _ 
          "and SUBSTRING(d.REG_PRI,LENGTH(d.XL_PRI) - 1,2) = 75 " 

      cmd = New MySqlCommand(strQ, con) 
      da = New MySqlDataAdapter(cmd) 
      ds = New DataSet 
      da.Fill(ds, "tblJO") 

      For r = 0 To ds.Tables("tblJO").Rows.Count - 1 
       Application.DoEvents() 
       colItemCode = ds.Tables("tblJO").Rows(r)(0).ToString 
       colDel_Date = ds.Tables("tblJO").Rows(r)(1).ToString 
       colReg_pri = ds.Tables("tblJO").Rows(r)(2).ToString 
       colXL_pri = ds.Tables("tblJO").Rows(r)(3).ToString 
       colLabel = ds.Tables("tblJO").Rows(r)(4).ToString 

       sumStyle = ds.Tables("tblJO").Rows(r)(5).ToString 
       sumJO = ds.Tables("tblJO").Rows(r)(6).ToString 
       sumNo = ds.Tables("tblJO").Rows(r)(7).ToString 
       colDays = ds.Tables("tblJO").Rows(r)(8).ToString 

       'for the number to decimal 
       colReg_pri = FormatNumber(colReg_pri, 2) 
       colXL_pri = FormatNumber(colXL_pri, 2) 


       'get the total quantity and the branch quantity 
       get_TotalQty_and_BranchQty() 

       'if the branch quantity is zero 
       If branchQty = 0 Then 
        PCT = "N\A" 

       Else 
        'compute the percent sold 
        PCT = totalQty 

       End If 

       dvList.Rows.Add(False, colItemCode, PCT, colDel_Date, Format(Date.Now, "yyyy-MM-dd"), colDays, _ 
           colReg_Pri, colXL_Pri, colLabel, "JO", dtpSDate.Text, dtpEDate.Text, _ 
           clbBranch.CheckedItems(i).ToString, mkType) 
      Next 

     Next 

    Catch ex As Exception 
     MsgBox(ex.Message) 
    Finally 
     con.Close() 
    End Try 

End Sub 

我使用索引在我的表,也可以增加application.doevents。有什么我可以做的,使这个代码更快地填充datagridview?我试图搜索关于使datagridview doublebuffered,因为我在线阅读它,我会使datagridview更快,但我不知道如何使用它。

任何帮助将不胜感激,谢谢。

+0

看来你需要看看这个链接:http://stackoverflow.com/questions/5181777/use-of-application-doevents关于更快地做到这一点,我不认为有很多方法以加速此代码。你最可能想要的是DGV的人口不干扰GUI(因此不会冻结你的表单);在这种情况下,您应该依赖backgroundworker(例如,从后台线程中的数据库中检索数据,将其存储到DataTable中,并且一旦完成这些操作,就可以通过DataSource将其传输到DGV,或者逐行添加)。 – varocarbas

回答

0

你的问题是使用

Application.DoEvents 

我不打算进入的为什么这是不好的做法详细信息,请尝试使用背景工人来代替。

+0

好吧,先生,我将搜索关于后台工作者,谢谢:) – Matthew