Click here to Skip to main content
12,397,480 members (60,649 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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
            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 22-Nov-12 21:12pm
dcba1894
Updated 12-Dec-12 21:29pm
v3
Rate this: bad
 
good
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
           Else
               If InputQuery = "" Then
                   Return QueryForProcess
               Else
                   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, 
           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
  Permalink  
Comments
01010RAJ 13-Dec-12 4:47am
   
Sure,it will work
Rate this: bad
 
good
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.
  Permalink  

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


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2016
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