Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

vb.net - How to select data with specific values/rows in search textbox?

Here's my table:

EmployeeNo User Item
108 Jeff Mouse
109 Ven Keyboard
110 Jeff Monitor
question from:https://stackoverflow.com/questions/65833375/how-to-select-data-with-specific-values-rows-in-search-textbox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Supposing that your are using _dataTable1 to get your table, and your filter name is Filter1

you can do as follow:

Private Sub txtSearchbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchbox.TextChanged

Dim Filter1 As String
Filter1 = "EmployeeNo LIKE '%" & txtSearchbox.Text & "%' OR " & _
          "User LIKE '%" & txtSearchbox.Text & "%' OR " & _
          "Item LIKE '%" & txtSearchbox.Text & "%'"

    ' Sort bonus ;)
    Dim Sort1 As String = " User  ASC"

    _dataTable1.DefaultView.RowFilter = Filter1
    _dataTable1.DefaultView.Sort = Sort1    

    'Bind your DataGridView1 to your _dataTable1
    DataGridView1.DataSource = _dataTable1
End Sub

Edit:

Supposing that previously you declared your _dataTable1 public and already loaded the data inside with your query

Public _dataTable1 As New DataTable()

Public Sub New()

' Do you query here
Dim query As string = "Select [EmployeeNo], [User], [Item] "
Using con as oledbconnection = new oledbconnection("datasourceofdatabase")
....
....
If dt1.Rows.Count > 0 Then
                _dataTable1 = dt1
....
....
End Sub

I suggest you to take a look to the following : VB.NET DataGridView binding - Sql Server there is simple example that work very well and easy to understand. Not only for DataGridView but for all VB.net.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...