Hi every1 !
i want to make a search through a datagrid using a textbox. i want the search to be like in the cell phone, i.e whenever u type an alphabetfor e.g P it retrieves all names with P.
i just want the textbox to search in one column only.
thanks .. waiting 4 n answer

Dani AI

Generated

Short answer: there are two clean options — client-side filtering (fast and simple for small/local datasets) or server-side filtering with a LIKE query (better for large tables). started with a DataView equality filter and suggested LIKE; both are valid but pick the one that fits your dataset size and UX needs.

Client-side (typeahead, immediate feedback)

  • Fill the DataSet once and filter its DefaultView as the user types. This avoids repeated DB round-trips and is ideal for a few thousand rows. Make sure the DataTable.CaseSensitive property is set the way you want (default is usually case-insensitive). Example pattern for "starts with":
' client-side starts-with filter
Dim key As String = TextBox1.Text.Trim()
ds.Tables("PatientDetails").DefaultView.RowFilter = "FirstName LIKE '" & key & "%'"
DataGridView1.DataSource = ds.Tables("PatientDetails").DefaultView

Server-side (recommended for large tables)

  • Run a parameterized query so the database only returns matching rows. For OleDb use positional parameters (?), e.g.:
cmd.CommandText = "SELECT * FROM PatientDetails WHERE FirstName LIKE ?"
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("?", key & "%")
da.SelectCommand = cmd
da.Fill(ds, "PatientDetails")

Notes & troubleshooting

  • Access wildcards can bite you: Access (Jet) in ANSI-89 mode uses "*" while ANSI-92/standard SQL uses "%". If a query returns nothing, try the other wildcard or use a parameterized approach.
  • If a control name (like dgAuthor) is underlined, check the actual DataGridView name in the designer and the DataMember you bind to.
  • If you hit errors in VS2010 on a 64-bit machine, Jet OLEDB.4.0 is 32-bit only — set the project target to x86 (Project → Properties → Compile → Advanced Compile Options → Target CPU = x86) or install/use the ACE provider.
  • For MySQL (as asked) use Connector/NET and standard "%" wildcards in LIKE.
  • For performance on server-side searches, index the FirstName column and avoid building SQL with string concatenation (use parameters).

Recommended Answers

All 13 Replies

connect with database?what it is?
if used database binding in datagrid try with LIKE operator to searching more thorough.

Yes i have used a database...
can u help me ?

what kind of database?

Access

did u have the search function before?
use like operator in your search clause..

Hi!
In this case I think subscribing in the following site will give u a lot: snipped
It gives an opportunity to web crawlers to obtain more precise information about web sites, and this is a democratic apporach (as to me).

Good luck

hi can u help me ?

here is my work. whenever i enter an alphabet in the textbox for eg 'g' or any letter i want the datagrid to start searching all names strating from g or any other letter. but here it search only the names for ex 'geeta' otherwise u get nothing if it does not find the name. Need Help !

Public Class SearchPatient

Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String

Private Sub PatientDetailsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.PatientDetailsBindingSource.EndEdit()
Me.PatientDetailsTableAdapter.Update(Me.DentistryDataSet.PatientDetails)

End Sub

Private Sub SearchPatient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DentistryDataSet.PatientDetails' table. You can move, or remove it, as needed.
Me.PatientDetailsTableAdapter.Fill(Me.DentistryDataSet.PatientDetails)
Me.MaximizeBox = False
Me.MinimizeBox = False
Form1.HomeToolStrip.Enabled = True
' connection
con.ConnectionString = "Provider = Microsoft.JET.OLEDB.4.0;Data Source= C:\Dentistry.mdb"
con.Open()

sql = "Select * From PatientDetails "
da = New OleDb.OleDbDataAdapter(sql, con)

da.Fill(ds, "Dentistry")
con.Close()

End Sub


Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim a = TextBox1.Text
Dim dv As New DataView(ds.Tables(0))
dv.RowFilter = "FirstName = '" & a & "'"
PatientDetailsDataGridView.DataSource = dv
End Sub
End Class

Hi Geeta...
I was Modified your code, please looking carefully :
I used One textBox Named txtSearchKey and Button Named btnSearch
i made LoadData procedure to display data in datagrid at first form loaded.
Look for declaration and assignment...

Dim con As New OleDb.OleDbConnection
    Dim cmdOle As New OleDb.OleDbCommand
    Dim dsOle As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim dtOle As New DataTable
    Dim sql As String

    Private Sub LoadData()
        con.ConnectionString = ("Provider = Microsoft.JET.OLEDB.4.0;Data Source= C:\Dentistry.mdb")
        Try
            cmdOle = con.CreateCommand
            cmdOle.CommandText = "SELECT * FROM PatientDetails" ' This Table name
            da.SelectCommand = cmdOle
            da.Fill(dsOle, "PatientDetails") ' This Table Name
            dgAuthor.DataSource = dsOle
            dgAuthor.DataMember = "PatientDetails" ' This table Name
            dgAuthor.ReadOnly = True
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Koneksi Error !!")
        End Try

    End Sub
    Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        con.ConnectionString = ("Provider = Microsoft.JET.OLEDB.4.0;Data Source= C:\Dentistry.mdb")
        Try
            dsOle.Clear()
            dtOle.Clear()
            cmdOle = con.CreateCommand
            cmdOle.CommandText = "SELECT * FROM PatientDetails where FirstName LIKE '%" & Trim(txtSearchKey.Text) & "%'" 
            da.SelectCommand = cmdOle
            da.Fill(dsOle, "PatientDetails") ' This Table name
            dgAuthor.DataSource = dsOle
            dgAuthor.DataMember = "PatientDetails" ' This Table name
            dgAuthor.ReadOnly = True

        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Koneksi Error !!")
        End Try
    End Sub

So if you input eg : "w" in txtSearchKey...program will search every name that it include w.
you can try for "wi", program will search name that it include "wi" in their name.
I provide attachment so u can see the result...
Hope this helps you...

But the dgAuthor is underline it gives me an error

dgAuthor is your Datagrid Name.
what message of error?

I post an example in attachment.
Hope it help you.

Hi Geeta...
I was Modified your code, please looking carefully :
I used One textBox Named txtSearchKey and Button Named btnSearch
i made LoadData procedure to display data in datagrid at first form loaded.
Look for declaration and assignment...

Dim con As New OleDb.OleDbConnection
    Dim cmdOle As New OleDb.OleDbCommand
    Dim dsOle As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim dtOle As New DataTable
    Dim sql As String

    Private Sub LoadData()
        con.ConnectionString = ("Provider = Microsoft.JET.OLEDB.4.0;Data Source= C:\Dentistry.mdb")
        Try
            cmdOle = con.CreateCommand
            cmdOle.CommandText = "SELECT * FROM PatientDetails" ' This Table name
            da.SelectCommand = cmdOle
            da.Fill(dsOle, "PatientDetails") ' This Table Name
            dgAuthor.DataSource = dsOle
            dgAuthor.DataMember = "PatientDetails" ' This table Name
            dgAuthor.ReadOnly = True
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Koneksi Error !!")
        End Try

    End Sub
    Private Sub Search_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        con.ConnectionString = ("Provider = Microsoft.JET.OLEDB.4.0;Data Source= C:\Dentistry.mdb")
        Try
            dsOle.Clear()
            dtOle.Clear()
            cmdOle = con.CreateCommand
            cmdOle.CommandText = "SELECT * FROM PatientDetails where FirstName LIKE '%" & Trim(txtSearchKey.Text) & "%'" 
            da.SelectCommand = cmdOle
            da.Fill(dsOle, "PatientDetails") ' This Table name
            dgAuthor.DataSource = dsOle
            dgAuthor.DataMember = "PatientDetails" ' This Table name
            dgAuthor.ReadOnly = True

        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Koneksi Error !!")
        End Try
    End Sub

So if you input eg : "w" in txtSearchKey...program will search every name that it include w.
you can try for "wi", program will search name that it include "wi" in their name.
I provide attachment so u can see the result...
Hope this helps you...

Hi there,

I know this is an old post but I have a question.

I tried your code but got a strange error (see attachment).

I use Visual Studio 2010, don't if that causes the error.

Any idea?

what if i am using mysql database?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.