Pergunta

Eu tenho tentado para destacar um resultado de pesquisa na DataTable. Primeiro, eu loop através de linhas de DataTable e chamar uma função para procurar uma palavra-chave correspondente para destacar essas palavras, em seguida, atualizar a linha DataTable com uma nova string com palavras-chave destacadas.

Eu vincular a DataTable dtResult ao DataList. Funcionou muito bem até que eu adicionei este bloco de código para a função SearchDemo para destacar a palavra-chave:


    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

uma etiqueta dentro DataList ligado este "verso" mostra System.Data.DataRow coluna. Os shows de descanso os dados corretos.

Por favor, consulte o seguinte bloco de código:

.........................

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

............................................... ..........

Estas duas funções abaixo funciona bem.

    '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

Todas as outras linhas da DataTable dtResul são mostrados corretamente, exceto as linhas de coluna "verso" que eu tentei destacar as palavras-chave. Se eu remover um loop (para palavras-chave de destaque) no interior do SearchDemo, ele vai funciona bem. Alguém pode ter um olhar para estes códigos e apontar-me a direção certa, por favor?

Foi útil?

Solução

O seu texto de entrada é dtResult.Rows (i) .ToString que é "System.Data.DataRow".

Alterar esta linha:

Dim strTemp As String = dtResult.Rows(i).ToString

Para:

Dim strTemp As String = dtResult.Rows(i)("verse").ToString
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top