Results 1 to 11 of 11

Thread: Data grid view - search

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Data grid view - search

    Hi

    How to search the text in particular column in data grid view


    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data grid view - search

    Is this grid bound to a DataTable or some other data source? If so then you'd search the data source via a BindingSource, not the grid. If it's not bound then you simply loop through the rows and test the value in the column of interest each iteration.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Data grid view - search

    Hi,

    populate the grid
    Code:
     Private Sub PopulateSearchGrid1()
            Dim DaSearch As SqlDataAdapter
            Dim dtSearch As New DataTable
            Dim StrQuery As String
            Try
                StrQuery = "SELECT EmployeeMaster.EmpCode, EmployeeMaster.Name as EmpName,EmployeeMaster.DOB FROM EmployeeMaster"
                DaSearch = New SqlDataAdapter(StrQuery, Conn)
                DaSearch.Fill(dtSearch)
                dgvSearch.DataSource = dtSearch
                DaSearch.Dispose()
                dtSearch.Dispose()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    trying the searching the grid value:
    Code:
     Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
            Dim intLoop As Integer
            Dim intLength As Integer
            Dim intRowFound As Integer = 0
            intLength = Len(txtSearch.Text)
            If dgvSearch.RowCount > 1 Then
                For intLoop = 1 To dgvSearch.RowCount - 1
                    If LCase(txtSearch.Text) = LCase(Mid(dgvSearch.Rows(intLoop).Cells("EmpName").Value, 1, intLength)) Then
                        intRowFound = intLoop
                        'dgvSearch.Row = intRowFound
                        'dgvSearch.TopRow = intRowFound
                        Exit Sub
                    Else
                        'dgvSearch.Row = intRowFound
                        'dgvSearch.TopRow = intRowFound
                    End If
                Next
            End If
        End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data grid view - search

    So it's bound. In that case you should bind your DataTable to a BindingSource and the BindingSource to the grid, instead of the table directly to the grid. You can then call the Find method of the BindingSource to get the index of the first matching row.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Data grid view - search

    pls can u give me the detail description with code

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Data grid view - search

    To search the data in column .. i try to bound the data grid view

    Code:
     Private Sub PopulateDisplay()
            Dim dgvDisplayBindingSource As New BindingSource()
            Dim DtDisplay As New DataTable
            Dim StrDisplay As String = "SELECT EM.Empcode,EM.EmpName, EM.DOB as [Date of Birth],"
            StrDisplay = StrDisplay + " DM.DesigName as Designation, EM.Salary FROM EmployeeMaster EM"
            StrDisplay = StrDisplay + " LEFT JOIN DesignationMaster DM ON EM.Desigcode =dm.DesigCode"
            Dim Dadisplay As New OleDbDataAdapter(StrDisplay, Conn)
            Dadisplay.Fill(DtDisplay)
            Try
                dgvDisplayBindingSource.DataSource = DtDisplay
                dgvDisplayBindingSource.DataSource = dgvDisplayBindingSource
                'dgvDisplay.DataSource = DtDisplay
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    error: BindingSource cannot be its own data source. Do not set the DataSource and DataMember properties to values that refer back to BindingSource.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data grid view - search

    What did I say previously?
    bind your DataTable to a BindingSource and the BindingSource to the grid
    You've got the first half of that right.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Data grid view - search

    now it is ok
    Code:
            dgvDisplayBindingSource.DataSource = DtDisplay
            dgvDisplay.DataSource = dgvDisplayBindingSource
    now tell me how to search the data

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data grid view - search

    I suggest that you read the documentation for the BindingSource class first, so you understand what it is and what it does, then post back if you are still in need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Data grid view - search

    Hi,

    According to your guide line .. I try and result will be achived but i want to display the grid according to the value type in text box means..
    if i type p if any word start with p than cursor will move to that row..

    Code:
      Private Sub txtSearchName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchName.TextChanged
            Try
                Dim FoundIndex As Integer = dgvDisplayBindingSource.Find(Trim(lblSearch.Text), txtSearchName.Text)
                'dgvDisplay.Rows(FoundIndex).Cells(Trim(lblSearch.Text)).Value
                If FoundIndex > -1 Then
                    MsgBox("Record Found...")
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data grid view - search

    The Find method takes two parameters: the name of the column to search on and the value of the column to search for. Are those the values you're passing? Also, Find will only look for exact matches. It's no use if you want to find a row where the column value starts with a particular substring.

    In that case you should use the Select method of the bound DataTable, which will allow you to filter using wildcards. Once you've got the first matching row you can use the BindingSource.Find method to get the index of the grid row that has the corresponding ID value:
    vb.net Code:
    1. Dim rows As DataRow() = myDataTable.Select(String.Format("{0} LIKE '{1}*'", columnName, columnValue), myBindingSource.Sort)
    2.  
    3. If rows.Length > 0 Then
    4.     Dim rowIndex As Integer = myBindingSource.Find("ID = " & rows(0)("ID"))
    5. End If
    Alternatively, you could set the Filter property of the BindingSource and all rows that don't match will be filtered out of grid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width