2016-10-10 18 views
0

テキストボックスを使用してデータグリッドを検索する簡単な方法があるのでしょうか。私はそれについていくつか読んだが、私が見つけたすべての例はVSの古いバージョンのものだった。古いバージョンのVSは、データソースに接続するために多くのコードを使用しているため、多くのコードを使用して検索しているようです。私はこれを単純化できると思っていますか?VB.Net 2015 DataGridを検索するテキストボックス

Private Sub Baseball_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    Me.PlayersTableAdapter.Fill(Me.BaseballDataSet.Players) 

End Sub 

データをグリッドに読み込みます。テーブルには、野球選手の名前と打率の平均が含まれています。私は、テキストボックスとボタンを使用して名前と別のものを検索して打撃平均で検索することができるようにしたいと考えています。

+0

あなたは 'DataSource'からビューを作成し、そのビューにフィルタを適用し、そのビューを' DataSource'に適用することができます。それはあなたの質問に答えるために、*** DataGridView'を検索する簡単な方法です。特定の問題を解決するためにまずあなたの試行を見せてください。 – Codexer

+0

'DataSource'を変更する必要はありません。タイプされた 'DataSet'を使用していますので、既に' BindingSource'を介してバインドしていることを願っています。 'BindingSource'の' Filter'プロパティを設定してデータをフィルタリングすることができます。 'BindingSource'で' Find'を呼び出して、一致する行のインデックスを取得し、それを 'Position'プロパティに代入して選択することもできます。 – jmcilhinney

答えて

0

これは私のサンプルコードのものです。私はTextboxと1つを持っていますButton

Private Sub srchBtn_Click(sender As Object, e As EventArgs) Handles srchBtn.Click 
    On Error GoTo wewe 

    If txtSearch.Text = "" Then 
     Call notFound() 
     Exit Sub 

    Else 

     Dim cantFind As String = txtSearch.Text 

     SampleBindingSource.Filter = "(Convert(Number_of_Employees, 'System.String') LIKE '" & txtSearch.Text & "') OR (ewan LIKE '" & txtSearch.Text & "') OR (ko LIKE '" & txtSearch.Text & "') OR (sayo LIKE '" & txtSearch.Text & "') OR (hehehe LIKE '" & txtSearch.Text & "')" 

     If SampleBindingSource.Count <> 0 Then 
      With DataGridView1 
       .DataSource = SampleBindingSource 
      End With 
     Else 

      MsgBox(cantFind & vbNewLine & "The search item was not found!", MsgBoxStyle.Information, "Hey boss") 

      SampleBindingSource.Filter = Nothing 

      With DataGridView1 
       .ClearSelection() 
       .DataSource = SampleBindingSource 
      End With 


     End If 
    End If 


hey: 
    Exit Sub 

wewe: 

    MsgBox("Error Number " & Err.Number & vbNewLine & "Error Description " & Err.Description, MsgBoxStyle.Critical, "Reset Error!") 
    Resume hey 

End Sub 

Private Sub reset() 
    Dim txtS As TextBox = txtSearch 

    With txtS 
     .Text = "" 
     .Select() 

    End With 

    If DataGridView1.DataSource Is Nothing Then 
     Exit Sub 
    End If 

End Sub 

Private Sub notFound() 
    Dim txtS As TextBox = txtSearch 

    With txtSearch 
     .Text = "" 
     .Select() 
     .SelectAll() 

    End With 

    If DataGridView1.DataSource Is Nothing Then 
     Exit Sub 
    End If 
End Sub 

ボタンをクリックすると、特定の単語が見つかります。注:そのコードはButtonに入れてください。それは、正確に綴られた単語、または完全な単語のみを見つけます。

例:Anthonyという単語を探したいとします。単語全体を入力し、検索ボタンをクリックします。あなたがいずれかのボタンをクリックせずにアイテムを検索したい場合は

、あなたはこれを試すことができます。

Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged 
    Try 

     Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Yourdatabase.mdb;") 
      conn.Open() 
      Dim command As New OleDbCommand("SELECT * FROM yourtable WHERE (ID like @ID) OR (Sample like @Sample) OR (Sample like @Sample) OR (Sample like @Sample) OR (Sample like @Sample)", conn) 
      With command.Parameters 
       .AddWithValue("@Sample", TextBox5.Text) 
       .AddWithValue("@Sample", TextBox5.Text) 
       .AddWithValue("@Sample", TextBox5.Text) 
       .AddWithValue("@Sample", TextBox5.Text) 
       .AddWithValue("@Sample", TextBox5.Text) 
      End With 
      Dim adapter As New OleDbDataAdapter 
      Dim dt As New DataTable 
      adapter.SelectCommand = command 
      adapter.Fill(dt) 
      DataGridView.DataSource = dt 
      adapter.Dispose() 
      command.Dispose() 
      conn.Close() 
     End Using 
    Catch ex As Exception 
     MessageBox.Show(ex.Message, "ERROR4", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    End Try 
End Sub 

場所はTextChangedイベントでそのコードをTextboxに。注:接続文字列を使用しました。

これが役に立ちます。

関連する問題