Results 1 to 6 of 6

Thread: [2005] Search in Data Grid view

  1. #1

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

    [2005] Search in Data Grid view

    Pls guide how to search the data in grid on text change event ..

    Code:
     Private Sub txtSearchName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchName.TextChanged
            ''Move the cursor to the position -- if sigle word is match
            'lblSearch.Text - Column Name in which the Search should be done
            Dim intLoop As Integer
            Dim intLength As Integer
            Dim intRowFound As Integer
            intRowFound = 0
            intLength = Len(txtSearchName.Text)
            If dgvDisplay.RowCount > 1 Then
                For intRowFound = 1 To dgvDisplay.RowCount - 1
                    If LCase(txtSearchName.Text) = LCase(Mid(dgvDisplay.Rows(intLoop).Cells(Trim(lblSearch.Text)).Value, 1, intLength)) Then
                        intRowFound = intLoop
                        dgvDisplay.Rows.SharedRow(intRowFound)
                        dgvDisplay.Rows.GetRowState(intRowFound)
                        dgvDisplayBindingSource.Position = intRowFound
                        Exit Sub
                    Else
                        dgvDisplay.Rows.SharedRow(intRowFound)
                        dgvDisplay.Rows.GetRowState(intRowFound)
                        dgvDisplayBindingSource.Position = intRowFound
                    End If
                Next
            End If
    
            ''Match the txtsearchname.text with grid value and then move to position 
            'Try
            '    Dim Rows As DataRow() = DtDisplay.Select(String.Format("{0} LIKE '{1}*'", Trim(lblSearch.Text), Trim(txtSearchName.Text)), dgvDisplayBindingSource.Sort)
            '    If Rows.Length > 0 Then
            '        Dim FoundIndex As Integer = dgvDisplayBindingSource.Find(Trim(lblSearch.Text), txtSearchName.Text)
            '        dgvDisplayBindingSource.Position = FoundIndex
            '    End If
            'Catch ex As Exception
            '    MsgBox(ex.Message)
            'End Try
        End Sub

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

    Re: [2005] Search in Data Grid view

    What do you mean by "search"? What exactly is it that you're trying to find? Please provide a proper description this time because we shouldn't have to try to work it out from the code.
    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: [2005] Search in Data Grid view

    If i put a data in txtsearchname.text, it will search the data in populate data grid view.

    ex: "a" will move the cursor to the row where "a"
    "ab" will move the cursor to the row where "ab" , if not find than cursor will at position of "a"

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

    Re: [2005] Search in Data Grid view

    So you're talking about a specific column, or the whole 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

  5. #5

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

    Re: [2005] Search in Data Grid view

    specifix column

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

    Re: [2005] Search in Data Grid view

    Quote Originally Posted by asm
    specifix column
    And when I asked for a proper description you chose to omit that part? If you would like help then please provide a proper, complete description of the problem we're supposed to be solving. It doesn't take that much time or effort and it can avoid a lot of wasted time in our trying to draw the information out of you or making assumptions that are invalid and therefore providing useless advice.

    There is no specific functionality available to do that. You just have to loop through the rows and test each one until you find a match, e.g.
    vb.net Code:
    1. Dim row As DataRowView
    2. Dim substring As String = Me.TextBox1.Text
    3.  
    4. For index As Integer = 0 To Me.BindingSource1.Count - 1 Step 1
    5.     row = DirectCast(Me.BindingSource1(index), DataRowView)
    6.  
    7.     If CStr(row("Name")).StartsWith(substring) Then
    8.         Me.BindingSource1.Position = index
    9.         Exit For
    10.     End If
    11. Next index
    An alternative would be to filter out all rows that don't match, which is easier still:
    vb.net Code:
    1. Me.BindingSource1.Filter = String.Format("Name LIKE '{0}%'", Me.TextBox1.Text)
    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