如何在VB.Net上的DataGridView上修复双输出

时间:2014-06-03 01:07:21

标签: mysql vb.net

我在后端使用MySQL。我的程序有两个datagridviews,一个用于employee表,另一个用于employee日志。问题是每当我将数据加载到两个datagridviews上时,数据都是重复的。例如,员工表上有4个数据,它将显示4个数据,然后它将在第一个4下方显示另一组数据。两个datagridviews就是这样做的。我的代码中似乎有什么问题?

Imports MySql.Data.MySqlClient
Public Class ViewMealLog


Dim dbDataSet As New DataTable
Dim dbDataSet2 As New DataTable

Public sConnection As New MySqlConnection

Private Sub load_table()

    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDA As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource


    Try
        sConnection.Open()
        Dim Query As String
        Query = "select * from swipe_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDA.SelectCommand = sqlCommand
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub

Private Sub load_table2()
    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDAX As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource


    Try
        sConnection.Open()
        Dim Query As String
        Query = "select emp_no, emp_firstnm, emp_midnm, emp_lastnm, schedtype from employee_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDAX.SelectCommand = sqlCommand
        SDAX.Fill(dbDataSet2)
        bSource.DataSource = dbDataSet2
        DataGridView2.DataSource = bSource
        SDAX.Update(dbDataSet2)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    load_table2()

    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDAX As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource
    Try
        sConnection.Open()
        Dim Query As String
        Query = "select emp_no, emp_firstnm, emp_midnm, emp_lastnm, schedtype from employee_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDAX.SelectCommand = sqlCommand
        SDAX.Fill(dbDataSet2)
        bSource.DataSource = dbDataSet2
        DataGridView2.DataSource = bSource
        SDAX.Update(dbDataSet2)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    load_table()

    sConnection = New MySqlConnection
    sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

    Dim SDA As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim bSource As New BindingSource
    Try
        sConnection.Open()
        Dim Query As String
        Query = "select * from swipe_table"
        sqlCommand = New MySqlCommand(Query, sConnection)

        SDA.SelectCommand = sqlCommand
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)

        sConnection.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        sConnection.Dispose()
    End Try
End Sub


Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    MainMenu.Show()
    Me.Hide()
End Sub


Private Sub txtSearch_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch.TextChanged
    Dim DV As New DataView(dbDataSet)
    DV.RowFilter = "CONVERT(emp_no, System.String) LIKE '%" & txtSearch.Text & "%' "
    DataGridView1.DataSource = DV
End Sub

Private Sub txtSearch2_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch2.TextChanged
    Dim DV As New DataView(dbDataSet)
    DV.RowFilter = "CONVERT(log_date, System.String) LIKE '%" & txtSearch2.Text & "%' "
    DataGridView1.DataSource = DV
End Sub



Private Sub txtSearch3_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch3.TextChanged
    Dim DV As New DataView(dbDataSet2)
    DV.RowFilter = "CONVERT(emp_no, System.String) LIKE '%" & txtSearch3.Text & "%' "
    DataGridView2.DataSource = DV
End Sub

Private Sub txtSearch4_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtSearch4.TextChanged

    Dim DV As New DataView(dbDataSet2)
    DV.RowFilter = "CONVERT(emp_lastnm, System.String) LIKE '%" & txtSearch4.Text & "%' "
    DataGridView2.DataSource = DV
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

为什么要在按钮点击事件中调用load_table和load_table2函数?尝试删除函数调用,因为您的按钮单击事件已经查询数据库。

 Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
"'load_table2()<<-----"

     sConnection = New MySqlConnection
     sConnection.ConnectionString = "server=localhost;userid=root;password=;database=cph;Convert Zero Datetime=True"

     Dim SDAX As New MySqlDataAdapter
     Dim sqlCommand As New MySqlCommand
     Dim bSource As New BindingSource
     Try
         sConnection.Open()
         Dim Query As String
         Query = "select emp_no, emp_firstnm, emp_midnm, emp_lastnm, schedtype from employee_table"
         sqlCommand = New MySqlCommand(Query, sConnection)

         SDAX.SelectCommand = sqlCommand
         SDAX.Fill(dbDataSet2)
         bSource.DataSource = dbDataSet2
         DataGridView2.DataSource = bSource
         SDAX.Update(dbDataSet2)

         sConnection.Close()

     Catch ex As MySqlException
         MessageBox.Show(ex.Message)
     Finally
         sConnection.Dispose()
     End Try
 End Sub

更新

或者喜欢这个

 Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
      load_table2()    
 End Sub