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

How to set forecolor for a specific cell in a GridViewHyperlinkColumn

3 Answers 199 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gone2TheDogs
Top achievements
Rank 1
Iron
Veteran
Gone2TheDogs asked on 13 May 2020, 02:22 PM

I have a grid where a hyperlink sometimes needs to be displayed as red. How is this done?

The following doesn't work.

Private Sub dgvXref_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles dgvXref.CellFormatting
If Not IsNothing(lXrefRowIdx_RAInHistory) andalso lXrefRowIdx_RAInHistory.Contains(e.RowIndex) Then
e.CellElement.ForeColor = Color.Red
End If
End Sub

3 Answers, 1 is accepted

Sort by
0
Gone2TheDogs
Top achievements
Rank 1
Iron
Veteran
answered on 13 May 2020, 07:32 PM

I resolved it using TryCast.

 

Private Sub dgvXref_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles dgvXref.CellFormatting
    If e.ColumnIndex = 0 andalso Not IsNothing(lXrefRowIdx_RAInHistory) andalso lXrefRowIdx_RAInHistory.Contains(e.RowIndex) Then
        Dim cell As GridHyperlinkCellElement = TryCast(e.CellElement, GridHyperlinkCellElement)
        cell.ContentElement.ForeColor = Color.Red
    End If
End Sub
0
Nadya | Tech Support Engineer
Telerik team
answered on 14 May 2020, 03:54 PM

Hello,

I am glad that you managed to find a solution to your problem. The CellFormatting is the appropriate event to customize the style of the cells. Casting the CellElement to the appropriate cell type is the right way to ensure that the desired cell will be customized.

Note, since RadGridView uses UI virtualization, its cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering. In order to prevent applying the formatting to other columns' cell elements, all customization should be reset for the rest of the cell elements. Do not forget to provide an else clause in the CellFormatting event and reset the visual changes. More information is available here: https://docs.telerik.com/devtools/winforms/controls/gridview/cells/formatting-cells.

I hope this information helps. Should you have other question I will be glad to help.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Gone2TheDogs
Top achievements
Rank 1
Iron
Veteran
answered on 14 May 2020, 04:18 PM

Thank you for the confirmation! 

I would have missed adding the ELSE statement. Thank you for the reminder and explanation.

 

 

Tags
GridView
Asked by
Gone2TheDogs
Top achievements
Rank 1
Iron
Veteran
Answers by
Gone2TheDogs
Top achievements
Rank 1
Iron
Veteran
Nadya | Tech Support Engineer
Telerik team
Share this question
or