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

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.