你好,我是vb.net编程的初学者,在使用textbox搜索或过滤datagridview时遇到了问题。我有一个用于搜索的文本框和一个用于检索值(即学生和车辆)的两个按钮。当点击student按钮时,显示student的数据,与vehicle相同,但当我将“dim dtaset as new datatable”移到students、vehicle和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
1条答案
按热度按时间bkkx9g8r1#
将数据集绑定到datagrid后,可以尝试以下代码段: