Click here to Skip to main content
16,020,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to filter datagridview. I have one textbox. When I enter value in that textbox datagridview should automatically get filter as per text in textbox.. I load data in datagridview by using following code:

VB
i = 0
rs.Open("select * from prtydtl where gkhass is not null ORDER BY prtynm ASC ", con, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        While Not rs.EOF
            prtygridview.RowCount = prtygridview.RowCount + 1
            prtygridview.Item(1, i).Value = rs.Fields("prtynm").Value
            i = i + 1
            rs.MoveNext()
        End While
rs.Close()



I don't want it by binding method...
I want to filter the list that s displayed on screen
Posted
Updated 12-Dec-12 21:29pm
v3

In my opinion:
1st-> U have to create a function to filter.Like this

VB
Private Function GetFilterQuery() As String
    Dim FilterString As String = ""
    Dim ValueString As String
    ValueString = UrTextBoxName.Text.Trim
    If ValueString <> "" Then
    FilterString = GetQueryWithLogic(FilterString, "TextBoxValue LIKE '%" + ValueString + "%'")
    End If
    Return FilterString
    End Function


Logic of GetQueryWithLogic
VB
Public Function GetQueryWithLogic(ByVal InputQuery As String, ByVal QueryForProcess As String) As String
           If (QueryForProcess = "") Then
               Return InputQuery
           Else
               If InputQuery = "" Then
                   Return QueryForProcess
               Else
                   Return InputQuery + " AND " + QueryForProcess
               End If
           End If
       End Function


2nd:Create this
VB
Public Sub FilterOutDataViewData(ByRef Dv As DataView, ByVal RowFilter As String)
           If Dv Is Nothing Then Exit Sub
           Dv.RowFilter = RowFilter
       End Sub

Now create a event for textchanged

VB
Private Sub TxtFilter_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles urtextBoxName.TextChanged, 
           Try
               RenfroCommonModule.FilterOutDataViewData(DV, GetFilterQuery())
           Catch ex As Exception

           End Try
       End Sub


Quote:
and on the event of urtextbox property select textchanged event and add TxtFilter_TextChanged
 
Share this answer
 
Comments
StackQ 13-Dec-12 4:47am    
Sure,it will work
Binding is much cleaner than what you're doing. Either way, you should filter in your SQL and update your grid, that's the only way that makes sense, let the database filter for you. That you want to write ugly code does not change that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900