美好的一天我是vb.net编程的初学者,我遇到了使用文本框搜索或过滤datagridview的问题。我有一个用于搜索的文本框和一个用于检索学生和车辆的值的两个按钮。单击学生按钮时,会显示学生的数据并且车辆的数据相同,但是当我移动" Dim dtaset As New DataTable"在学生,车辆和搜索文本的事件之外,搜索成功过滤数据值的数据,移动后也是一个问题,当点击按钮时,它会在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
答案 0 :(得分:1)
将数据集绑定到数据网格后,您可以尝试以下代码段:
Dim dv As DataView
dv = New DataView(yourDataset.Tables(0), "YourDataColumnName= '" & Me.txtSearchBox.Text & "' ", "YourDataColumnName", DataViewRowState.CurrentRows)
Me.YourDataGridView.DataSource = dv