0

please, am new to vb.net, i have a project and i want to use a textbox as a search box to search through the data on the datagrid view on my form. please kindly assist

3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by M.Waqas Aslam
1

u can write in the textbox textchange event with the sql query using the like condition....

'Open Connection
Try
   Dim myCommand As New OleDbCommand
   With myCommand
        .CommandText = "SELECT ID , (FirstName +' '+LastName) as Name FROM TableName where  ID like '" & Textbox1.Text & "%" + "' order by ID"
        .CommandType = CommandType.Text
        .Connection = Connection
   End With
   Dim dt As New DataTable
   dt.Load(myCommand.ExecuteReader)
   With DatagridView1
        .AutoGenerateColumns = True
        .DataSource = dt
   End With
Catch ex As Exception
   MsgBox("Error in select query: " + ex.Message)
End Try
'Close Connection
0

there is another method to do this is
use this code at the textbox textchange event

dim con as new sqlconnection("connection string")
dim dt as new datatable
con.open()
dim da as new sqldataadapter("select * from table1 where name like '" & textbox1.text & "%'",con)
da.fill(dt)
datagridview1.datasource = dt
con.close

hope this will helps you ,

Regards

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.