早上好,我是vb编程的初学者,但是在使用文本框搜索或过滤datagridview时遇到问题。我有一个用于搜索的文本框和一个用于检索值的两个按钮,即学生和车辆。当学生按钮单击时,显示的学生数据与车辆的数据相同,但是当我将 Dim dtaset As New DataTable移动到学生,车辆和searchtxt事件之外时,搜索成功过滤了datavalues数据,也存在问题同样,在移动之后,单击按钮会在datagridview中添加该按钮的数据,但是在第一个按钮之后单击另一个按钮时,数据不会替换前一个按钮,它只会添加将扩展datagrid列的列。这是我的代码预先谢谢您
Good day I am a beginner at vb programming and I'm having a problem about searching or filtering the datagridview using textbox. I have a one textbox for searching and a two button for retrieving values which is the student and vehicle. When the student button clicked the data for student is displayed and same for vehicle but when I move the "Dim dtaset As New DataTable" outside the events of students,vehicle and searchtxt the search is successfully filtering the data of the datavalues also theres a problem too after moving, when click the button it adds the data for that button in datagridview but when the other button is click after the first button the data not replaced the previous one, it will just add the columns that will extend the datagrid columns. Here is my code Thank you in advance
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推荐答案
将数据集绑定到datagrid之后,您可以尝试以下代码段:
After binding your dataset to the datagrid, you can try the following code snippet:
Dim dv As DataView dv = New DataView(yourDataset.Tables(0), "YourDataColumnName= '" & Me.txtSearchBox.Text & "' ", "YourDataColumnName", DataViewRowState.CurrentRows) Me.YourDataGridView.DataSource = dv更多推荐
使用文本框VB.net在Datagridview中搜索
发布评论