Click here to Skip to main content
12,953,223 members (77,528 online)
Rate this:
Please Sign up or sign in to vote.
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:

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
        End While

I don't want it by binding method...
I want to filter the list that s displayed on screen
Posted 22-Nov-12 21:12pm
Updated 12-Dec-12 21:29pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

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
Public Function GetQueryWithLogic(ByVal InputQuery As String, ByVal QueryForProcess As String) As String
           If (QueryForProcess = "") Then
               Return InputQuery
               If InputQuery = "" Then
                   Return QueryForProcess
                   Return InputQuery + " AND " + QueryForProcess
               End If
           End If
       End Function

2nd:Create this
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

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

and on the event of urtextbox property select textchanged event and add TxtFilter_TextChanged
01010RAJ 13-Dec-12 4:47am
Sure,it will work
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,920

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100