显示的Datagridview数据与datagriview具有的数据

时间:2017-05-09 12:45:34

标签: vb.net datagridview serial-port

我有一个主要表单(form1),除其他外,它等待接收信息

Private Sub SerialPort1_DataReceived(sender As System.Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim output As String
    output = SerialPort1.ReadLine()
    Converter(output) 'format what was received
    Submit() 'submit into db
    Form3.BackgroundWorker1.RunWorkerAsync()
    Thread.Sleep(2000)
    Application.DoEvents()
End Sub

并将信息处理/提交到数据库中,如果form3处于打开状态(该条件尚未测试,但我测试的所有时间都确保在通过串口发送数据之前打开form3)它应该更新form3中的datagridview。

Public Class Form3
        Delegate Sub dgvDelegate()
        Sub fill()
            tabela_dispensa.Rows.Clear()
            Conectar(conn, myCommand) ' connect to db
            myCommand.CommandText = "select dispensa.*,produto.Descrição from dispensa join produto on dispensa.PLU = produto.PLU"
            result = myCommand.ExecuteReader
            While result.Read
                tabela_dispensa.Rows.Add(result(0), result(5), result(1), result(2), result(3), result(4))
            End While
            Desconectar(conn) ' disconnect from db
        End Sub
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            BackgroundWorker1.RunWorkerAsync()
            'Conectar(conn, myCommand)
            'myCommand.CommandText = "select dispensa.*,produto.Descrição from dispensa join produto on dispensa.PLU = produto.PLU"
            'result = myCommand.ExecuteReader
            'While result.Read
            '    tabela_dispensa.Rows.Add(result(0), result(5), result(1), result(2), result(3), result(4))
            'End While
            'Desconectar(conn)
        End Sub

        Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            If InvokeRequired Then
                Invoke(New dgvDelegate(AddressOf fill))
            Else
                fill()
            End If
        End Sub
End Class

问题是datagridview仅在加载form3时才会更改。

我已经运行了一些测试,我可以看到,在我收到数据后打开form3之后,datagridview在我做某事之前就丢失了所有行,所以datagridview看起来是相同的,甚至丢失数据很奇怪。

因此,如果有人可以帮我更新datagridview会很棒,但如果有人能帮助我理解为什么datagridview会丢失所有数据但显示的内容仍然没有发生,我会很高兴。

重要的: Datagridview没有数据绑定。

对不起,如果文字有点乱。

2 个答案:

答案 0 :(得分:0)

在填充数据网格时尝试包含这些内容(将DataGridView1替换为您调用的任何内容)

        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        DataGridView1.AutoGenerateColumns = True
        Application.DoEvents()

答案 1 :(得分:0)

这种方法对我有用..

Dim command As String
DataGridView1.Columns.Clear()
Dim ds As New DataSet
Try
    Using connection As New SqlConnection(conString)
    command = "Your SQL Query"
    connection.Open()
    Dim da As New SqlDataAdapter(command, connection)
    connection.Close()
    ds.Tables.Add("ProductDetails")
    da.Fill(ds, "ProductDetails")
End Using
DataGridView1.DataSource = ds.Tables("ProductDetails")
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
DataGridView1.AutoGenerateColumns = True
Application.DoEvents()