[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
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.
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"
Re: [2005] Search in Data Grid view
So you're talking about a specific column, or the whole grid?
Re: [2005] Search in Data Grid view
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:
Dim row As DataRowView
Dim substring As String = Me.TextBox1.Text
For index As Integer = 0 To Me.BindingSource1.Count - 1 Step 1
row = DirectCast(Me.BindingSource1(index), DataRowView)
If CStr(row("Name")).StartsWith(substring) Then
Me.BindingSource1.Position = index
Exit For
End If
Next index
An alternative would be to filter out all rows that don't match, which is easier still:
vb.net Code:
Me.BindingSource1.Filter = String.Format("Name LIKE '{0}%'", Me.TextBox1.Text)