title: Searching in Datagridview using textbox VB.net
body: <p>Good day I am a beginner at vb.net 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</p>
  
  <pre><code>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 &gt;= 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 &gt; 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
  </code></pre>