0

目前,我的应用程序使用表达式上的 RowFilter 属性在 DataView 中搜索用户定义的字符串。目前我的代码看起来像这样:

Public Class MyClass
   Private custView As DataView
Private Sub form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   dsDataSet = <"DataAccessLayer call to SQL Stored Procedure">
   custView = New DataView(dsDataSet.Tables(0))
   custView.Sort = "Column Name"
   Me.C1FlexGrid1.DataSource = custView
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
    Dim searchText As String = txtSearch.Text
    Dim expression As String = "Column Name LIKE '" + searchText + "%'"

    custView.RowFilter = expression
    Me.C1FlexGrid1.DataSource = custView
End Sub
End Class

我的目标是修改它的行为,而不是过滤掉不符合搜索结果的行,而是保留所有行,但在用户在搜索框中键入时跳转到部分匹配的第一个实例。如果 DataView.Find() 支持通配符我会设置,但不幸的是它没有。

4

1 回答 1

1

我想出的解决方案是使用一些迭代逻辑。然而,这是在 DataView 已绑定到的对象上完成的,而不是在 DataView 本身上完成的。尽管可以修改此代码以完全做到这一点。

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        'Unselect all
        Me.C1FlexGrid1.Select(-1, -1, True)

        If txtSearch.Text <> "" And [column index] <> -1 Then
            'Typed text
            Dim s As String = txtSearch.Text.Trim.ToLower
            Dim column As Int32 = [column index] + 1

            'Recurse until match in first column is found
            For i As Integer = 0 To Me.C1FlexGrid1.Rows.Count - 1
                If C1FlexGrid1.GetData(i, column) <> Nothing Then
                    If C1FlexGrid1.GetData(i, column).ToString.ToLower.StartsWith(s) Then
                        Me.C1FlexGrid1.Select(i, column, True)
                        Exit Sub
                    End If
                Else

                    MsgBox("Error message", vbOKOnly, "NO MATCHES")

                    'Reset search criteria
                    Call ResetSearch()

                End If
            Next
            MsgBox("Error message", vbOKOnly, "NO MATCHES")
        End If
    End Sub
于 2015-04-13T17:19:37.863 回答