想过滤datagridview [英] want to filter datagridview

查看:52
本文介绍了想过滤datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想过滤datagridview。我有一个文本框。当我在该文本框中输入值时,datagridview应该根据文本框中的文本自动获取过滤器。我使用以下代码在datagridview中加载数据:



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

推荐答案

在我看来:

1st->你必须创建一个过滤功能。就像这个



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





GetQueryWithLogic的逻辑



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:创建此



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



现在为textch创建一个事件anged




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







引用:

以及关于urtextbox的事件属性选择文本更改了事件并添加了TxtFilter_TextChanged

and on the event of urtextbox property select textchanged event and add TxtFilter_TextChanged


绑定比你正在做的更清晰。无论哪种方式,您都应该在SQL中过滤并更新网格,这是唯一有意义的方法,让数据库为您过滤。你想写丑陋的代码不会改变它。
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.


这篇关于想过滤datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆