This is a migrated thread and some comments may be shown as answers.

Highlight cells or text in GridView

1 Answer 167 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Roberto Wenzel
Top achievements
Rank 2
Roberto Wenzel asked on 02 Sep 2008, 03:44 PM
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)
Me
.gvMessages.CurrentRow = Me.gvMessages.Rows(i)

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

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

Next
Next

End Sub

1 Answer, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 05 Sep 2008, 04:09 PM
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,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Roberto Wenzel
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
Share this question
or