Vb.Net DataGridView数据源未更新

时间:2016-12-09 11:45:57

标签: vb.net winforms datagridview

我有一个DataGridView在我的项目中使用DataTable作为源(dtOrders)。我正在尝试使用已编辑的行更新dtOrders并让DataGridView显示更新的行。我正在使用从BackGroundWorkerbgwWriteToDatabase

调用的以下代码
Public r as ArrayList = New ArrayList

Private Sub UpdateOrderTable()

    Try
        r.Add(RowNum)
        r.Add(bwOrderID)
        r.Add(bwClientID)
        r.Add(bwMaterials)
        r.Add(bwInstaller)
        r.Add(bwMaterialsCost)
        r.Add(bwInstallerCost)
        r.Add(bwElectricalCosts)
        r.Add(bwPipeFittingCosts)
        r.Add(bwSundriesCosts)
        r.Add(bwTotalCost)
        r.Add(bwNotes)

        dtOrders.Rows(r(0))("ID") = r(1)
        dtOrders.Rows(r(0))("ClientRef") = r(2)
        dtOrders.Rows(r(0))("Materials") = r(3)
        dtOrders.Rows(r(0))("Installer") = r(4)
        dtOrders.Rows(r(0))("MaterialsCost") = r(5)
        dtOrders.Rows(r(0))("InstallersCosts") = r(6)
        dtOrders.Rows(r(0))("ElectricalWork") = r(7)
        dtOrders.Rows(r(0))("PipeFittingValves") = r(8)
        dtOrders.Rows(r(0))("Sundries") = r(9)
        dtOrders.Rows(r(0))("TotalCosts") = r(10)
        dtOrders.Rows(r(0))("Notes") = r(11)

        bgwWriteToDatabase.ReportProgress(10, 10)

    Catch ex As Exception
        Debug.Print(ex.Message.ToString)
        Errorbits(3) = True
    End Try
End Sub


Private Sub bgwWriteToDatabase_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwWriteToDatabase.ProgressChanged
    If e.ProgressPercentage = 10 Then
        dgvViewOrders.DataSource = dtOrders
    End If
End Sub

只要在表单上按下“提交”按钮,就会执行代码。代码第一次运行时,DataGridView完美更新,它会正确显示新信息,一切都很好。

第二次按下“提交”按钮时,DataGridView上的信息不会更新。我很难理解为什么它第一次有效,但不是第二次。

我已经调试过,代码在第一次运行时运行没有错误,第二次运行时没有错误,BackGroundWorker在两个周期都以相同的方式执行但是第二次没有产生更新轮。

我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

我需要将import tkinter as tk # --- functions --- def subwindow(): # you don't need `global` # because you will not assign value #global var_option1 #global var_option2 window = tk.Toplevel(master) if var_option1.get(): # you need get() tk.Label(window, text="Option 1").pack() if var_option2.get(): # you need get() tk.Label(window, text="Option 2").pack() submit = tk.Button(window, text="Submit", command=window.destroy) submit.pack() # --- main --- master = tk.Tk() # it creates global variable so you don't need `global` # It has to be after tk.Tk() var_option1 = tk.BooleanVar(value=False) # or var_option2 = tk.BooleanVar() var_option2.set(False) # you need set() lbl_title = tk.Label(master, text="TICK BUTTONS") cb_option1 = tk.Checkbutton(master, text="Option1", variable=var_option1) cb_option2 = tk.Checkbutton(master, text="Option2", variable=var_option2) btn_submit = tk.Button(master, text="Submit", command=subwindow) lbl_title.pack() cb_option1.pack() cb_option2.pack() btn_submit.pack() master.mainloop() 添加到r.Clear()子...

的顶部