gpt4 book ai didi

mysql - 使用文本框 VB.net 在 Datagridview 中搜索

转载 作者:行者123 更新时间:2023-11-29 10:26:13 24 4
gpt4 key购买 nike

美好的一天,我是 vb.net 编程的初学者,我在使用文本框搜索或过滤 datagridview 时遇到问题。我有一个用于搜索的文本框和一个用于检索值的两个按钮,即学生和车辆。当学生按钮单击时,会显示学生的数据,车辆的数据也相同,但是当我将“Dim dtaset As New DataTable”移到学生、车辆和 searchtxt 事件之外时,搜索成功过滤了数据值的数据,也存在问题移动后,当单击该按钮时,它会在 datagridview 中添加该按钮的数据,但是当在第一个按钮之后单击另一个按钮时,数据不会替换前一个按钮,它只会添加将扩展 datagrid 列的列。这是我的代码,提前谢谢您

Private Sub searchtxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchtxt.TextChanged
con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"


Dim dtaset As New DataTable

Dim DV As New DataView(dtaset)
DV.RowFilter = String.Format("FirstName Like '%{0}%'", searchtxt.Text)
DataGridView1.DataSource = DV
End Sub

Public Sub studattenprint()
con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"

Dim adpter As New MySqlDataAdapter
Dim dtaset As New DataTable
Dim bsource As New BindingSource


Try
con.Open()
Dim query As String
query = "select idno as 'Student_ID',lastxt as 'LastName',firstxt as 'FirstName',middletxt as 'MiddleName',log as 'Status',timein as 'Timein',crse as 'Course',dates as 'Date' from dat.studdailyhistory"
cmd = New MySqlCommand(query, con)

adpter.SelectCommand = cmd
adpter.Fill(dtaset)
If dtaset.Rows.Count >= 0 Then
numlog.Text = dtaset.Rows.Count.ToString()
End If
bsource.DataSource = dtaset
DataGridView1.DataSource = bsource

''DataGridView Design
DataGridView1.AllowUserToAddRows = False ' Disabled or hide (*) Symbol...

DataGridView1.RowHeadersVisible = False 'To hide Left indicator..
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.SteelBlue 'Selection backcolor....
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGoldenrodYellow 'Alternating Backcolor.
DataGridView1.AllowUserToResizeRows = False 'Disabled row resize...
DataGridView1.ReadOnly = True
DataGridView1.MultiSelect = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.ShowRowErrors = False
DataGridView1.ShowCellErrors = False
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
DataGridView1.Sort(DataGridView1.Columns(7), System.ComponentModel.ListSortDirection.Descending)

adpter.Update(dtaset)
con.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub

Public Sub vehicattenprint()
con = New MySqlConnection
con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
Dim SDA As New MySqlDataAdapter
Dim dtaset As New DataTable
Dim bSource As New BindingSource


Try

con.Open()
Dim query As String

query = "select tag_no as 'Tag_No.',platenum as 'Plate no.',ownername as 'Owner_name',log as 'Status',timelog as 'Time_log',dates as 'Date' from dat.vehicdailyhistory"
cmd = New MySqlCommand(query, con)
SDA.SelectCommand = cmd
SDA.Fill(dtaset)
bSource.DataSource = dtaset
DataGridView1.DataSource = bSource

If dtaset.Rows.Count > 0 Then
numlog.Text = dtaset.Rows.Count.ToString()
End If

SDA.Update(dtaset)
DataGridView1.AllowUserToAddRows = False ' Disabled or hide (*) Symbol...

DataGridView1.RowHeadersVisible = False 'To hide Left indicator..
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.SteelBlue 'Selection backcolor....
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGoldenrodYellow 'Alternating Backcolor.
DataGridView1.AllowUserToResizeRows = False 'Disabled row resize...
DataGridView1.ReadOnly = True
DataGridView1.MultiSelect = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.ShowRowErrors = False
DataGridView1.ShowCellErrors = False
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
DataGridView1.Sort(DataGridView1.Columns(5), System.ComponentModel.ListSortDirection.Descending)

con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Sub

Private Sub vehicatten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles vehicatten.Click
vehicattenprint()
End Sub

Private Sub studatten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles studatten.Click
studattenprint()
End Sub

最佳答案

将数据集绑定(bind)到数据网格后,您可以尝试以下代码片段:

Dim dv As DataView

dv = New DataView(yourDataset.Tables(0), "YourDataColumnName= '" & Me.txtSearchBox.Text & "' ", "YourDataColumnName", DataViewRowState.CurrentRows)
Me.YourDataGridView.DataSource = dv

关于mysql - 使用文本框 VB.net 在 Datagridview 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48257565/

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