Click here to Skip to main content
Sign Up to vote bad
good
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
            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
smiths3837
Edited 12-Dec-12 21:29pm


2 solutions

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
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
Your Filters
Interested
Ignored
     
0 Christian Graus 578
1 Ron Beyer 326
2 Tadit Dash 253
3 samadhan_kshirsagar 229
4 OriginalGriff 198
0 Sergey Alexandrovich Kryukov 7,041
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 3,034


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