How to set forecolor for a specific cell in a GridViewHyperlinkColumn

4 posts, 0 answers
  1. Gone2TheDogs
    Gone2TheDogs avatar
    66 posts
    Member since:
    May 2016

    Posted 13 May Link to this post

    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
  2. Gone2TheDogs
    Gone2TheDogs avatar
    66 posts
    Member since:
    May 2016

    Posted 13 May in reply to Gone2TheDogs Link to this post

    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
  3. Nadya
    Admin
    Nadya avatar
    392 posts

    Posted 14 May Link to this post

    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.
  4. Gone2TheDogs
    Gone2TheDogs avatar
    66 posts
    Member since:
    May 2016

    Posted 14 May in reply to Nadya Link to this post

    Thank you for the confirmation! 

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

     

     

Back to Top