Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: VB.NET
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 22:12pm
Edited 12-Dec-12 22: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 at 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
0 Dnyaneshwar@Pune 719
1 CPallini 275
2 Sergey Alexandrovich Kryukov 250
3 Peter Leow 240
4 CHill60 235
0 Sergey Alexandrovich Kryukov 9,008
1 OriginalGriff 6,771
2 Peter Leow 4,262
3 Zoltán Zörgő 3,769
4 Richard MacCutchan 2,623

Advertise | Privacy | Mobile
Web03 | 2.8.150128.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2015
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