Highlight cells or text in GridView

2 posts, 0 answers
  1. Roberto Wenzel
    Roberto Wenzel avatar
    41 posts
    Member since:
    Sep 2012

    Posted 02 Sep 2008 Link to this post

    Hello again,
    I'm using RadGridView for WinForms Q2. I want to iterate through all cells and highlight text which I am searching for.
    I could live with changing the background of the found cell.
    The following approch produces errors.
    I would be grateful for some help. Cheers Roberto

    Private Sub SearchPhraseInGrid(ByVal searchphrase As String, Optional ByVal colname1 As String = "", Optional ByVal colname2 As String = "")

    Dim gv As RadGridView = Me.gvMessages
    Dim col As GridViewDataColumn
    Dim row As GridViewRowInfo
    Dim i As Integer
    For Each col In gv.Columns
    For Each row In gv.Rows
            If (row.Cellscol.FieldName).Value.ToString).Contains(searchphrase) = True Then

              i = Me.gvMessages.Rows.IndexOf(Me.gvMessages.CurrentRow)
    .gvMessages.CurrentRow = Me.gvMessages.Rows(i)

    .gvMessages.MasterGridViewTemplate.CurrentColumn = Me.gvMessages.Columns(col.Index)

    Me.gvMessages.GridElement.CurrentCell.BackColor = Color.Azure
    End If


    End Sub

  2. Nikolay
    Nikolay avatar
    1805 posts

    Posted 05 Sep 2008 Link to this post

    Hello Roberto Wenzel,

    Thank you for the question.

    I am not quite sure about the errors you are getting. However, it is not recommended to set properties for the appearance of the cells outside the CellFormatting event handler. If you want to set Me.gvMessages.GridElement.CurrentCell.BackColor = Color.Azure in your method, you should first check if CurrentCell is not null, but generally the BackColor property should be set only in the CellFormatting event handler, using e.CellElement which surely is not null.

    I have prepared a sample project with a similar to your implementation which demonstrates the correct behavior. Please note that this approach can show only one result (the first or the last), because only one column/row can be current at a time.
    My second project implements another approach, which puts a custom back color to all the cells of interest.

    If you have additional questions, feel free to contact me.

    Best wishes,
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top