簡體   English   中英

如何在vb.net中的datagridview中更快地填充

[英]How to populate faster in datagridview in vb.net

這是我用於填充datagridview的代碼:

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變為雙緩沖的信息,因為我在線閱讀了該書,我將使datagridview更快,但我不知道如何使用它。

任何幫助將不勝感激,謝謝。

您的問題正在使用

Application.DoEvents

我將不詳細介紹為什么這是不好的做法,請嘗試使用后台工作程序代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM