954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search DataGridView in VB.NET

Am developing a software for submission as project. As a part of it, am planning to create a Phonebook package. I created a database in access and i integrated it in VB.Net.

Information:
Database name : Contacts.accdb
DataTable : Contacts
Contents in table : First Name,Last Name, Phone, Address...
Platform : VB.NET in Visual Studio 2010

I integrated the access database in VB.NET using Data->Add new Data source and I successfully made it. Now I got a DataGridView. :) Now arises a question How to search a name in "First Name" Column? I just Googled to find row Filter. That appeared gr8 to me till I got a problem of adding new records. Once I applied Filter, I couldn't see the Addition of new records and I can't make selection process in DataGridView. Then I found that Filtering is not Searching. Any one to help me in searching the data in Dataview? I'll post my codes here:

Public Class Form1

    Private Sub ContactsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ContactsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ContactsDataSet)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ContactsDataSet.Contacts' table. You can move, or remove it, as needed.
        Me.ContactsTableAdapter.Fill(Me.ContactsDataSet.Contacts)

    End Sub

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp

        Dim dv As DataView = New DataView()
        dv.Table = ContactsDataSet.Contacts
        dv.RowFilter = "[First Name] Like '" & TextBox1.Text & "%'"
        ContactsDataGridView.DataSource = dv

    End Sub
      
End Class


I have a textbox named "TextBox1", Binding navigator that is automatically generated on copying the DataGrid named "ContactsBindingNavigator" and DataGrid cotaining Contacts table named "ContactsDataGridView". Any one help me.. Thanks!

Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 

If ur not loading Millions of records into grid then you can write custom code to serach for perticular record in grid using loop.

Pgmer
Master Poster
714 posts since Apr 2008
Reputation Points: 54
Solved Threads: 121
 

i think u try this code on the text change event of the textbox1 and on the form load event also ,

Sub MySaleInvoiceList()
        On Error Resume Next
        Dim myQuery As String 'defining a string 
        If textbox1.Text <> "" Then 'here i m chking that is user try to search? 
            myQuery = "select SalesID,SalesDate from Sales where  salesid = " & Val(textbox1.Text)'if user try to search then this code will execute
        Else
            myQuery = "select SalesID,SalesDate from Sales "'else this query will run

        End If

        MyConn.Open()
        Dim dt As New DataTable 
        Dim MyCommand As New SqlCommand(myQuery, MyConn)
        Dim myDataAdapter As New SqlDataAdapter(myQuery, MyConn)
        myDataAdapter.Fill(dt)
       DataGridView1.DataSource = dt
        MyConn.Close()
    End Sub


Hope This Will Helps U

Best Regards

M.Waqas Aslam

waqasaslammmeo
Posting Pro in Training
472 posts since Aug 2011
Reputation Points: 38
Solved Threads: 82
 

@waqasaslammmeo Thanks For ur reply.. I used the following code:

Dim str As String = TextBox1.Text
        Try
            If Me.TextBox1.Text.Trim(" ") = " " Then
            Else
                For i As Integer = 0 To DictionaryDataGridView.Rows.Count - 1
                    For j As Integer = 0 To Me.DictionaryDataGridView.Rows(i).Cells.Count - 1
                        If DictionaryDataGridView.Item(j, i).Value.ToString().ToLower.StartsWith(str.ToLower) Then
                            DictionaryDataGridView.Rows(i).Selected = True
                            DictionaryDataGridView.CurrentCell = DictionaryDataGridView.Rows(i).Cells(j)
                            Exit Try
                        End If
                    Next
                Next i
            End If
        Catch abc As Exception
            MessageBox.Show("Sorry!")
        End Try
Riteman
Light Poster
32 posts since Jun 2011
Reputation Points: 6
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: