gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:11:28 25 4
gpt4 key购买 nike

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

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

最佳答案

为什么要在按钮单击事件中调用 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

关于mysql - 如何修复 VB.Net 上 DataGridView 的双输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24005639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com