我一直在努力突出显示DataTable中的搜索结果。首先,我循环遍历DataTable的行并调用一个函数来查找匹配的关键字以突出显示这些单词,然后使用带有突出显示的关键字的新字符串更新DataTable行。
我将DataTable dtResult绑定到DataList。在我将此代码块添加到SearchDemo函数以突出显示关键字之前,它工作得很好:
For i = 0 To dtResult.Rows.Count - 1 Step 1
Dim strTemp As String = dtResult.Rows(i).ToString
strVerse = blHelper.Highlight(s, strTemp)
dtResult.Rows(i)("verse") = strVerse
Next
绑定此“verse”列的数据列表内的标签显示System.Data.DataRow。其余部分显示正确的数据。
Public Shared Function SearchDemo(ByVal s As String) As DataTable
Dim dtResult As New DataTable
dtResult = SearchDetail(s)
Dim i As Integer = dtResult.Rows.Count
For i = 0 To dtResult.Rows.Count - 1 Step 1
Dim strTemp As String = dtResult.Rows(i).ToString
strVerse = blHelper.Highlight(s, strTemp)
dtResult.Rows(i)("verse") = strVerse
Next
Return dtResult
End Function
.........................................................
下面这两个函数工作正常。
'Highlight the keywords in the returned result
Public Shared Function Highlight(ByVal Search_Str As String, ByVal InputTxt As String) As String
' Setup the regular expression and add the Or operator.
Dim RegExp As Regex = New Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase)
' Highlight keywords by calling the MatchEvaluator delegate each time a keyword is found.
Highlight = RegExp.Replace(InputTxt, New MatchEvaluator(AddressOf ReplaceKeyWords))
' Set the Regex to nothing.
RegExp = Nothing
End Function
Public Shared Function ReplaceKeyWords(ByVal m As Match) As String
Return "<b>" & m.Value & "</b>"
End Function
DataTable DTResult的每一行都会正确显示,除了我试图突出显示关键字的“verse”列的行。如果我在SearchDemo中删除一个循环(突出显示关键字),它就可以正常工作。
有人能看一下这些代码并给我指出正确的方向吗?