Conditional Formatting in Radgridview

4 posts, 0 answers
  1. Brent
    Brent avatar
    4 posts
    Member since:
    Jun 2012

    Posted 26 Sep Link to this post

    I'm having an issue with my project. I have a RadGridView populated by a Stored Procedure, I have some Cellformatting in place.
    I'm getting some odd behaviour when scrolling, the formatting is being applied to more than just the first 2 columns and the more you scroll the worse it gets.

    I'm using an older version but we recently purchased the latest version of telerik for winforms to test and I'm getting the same behaviour in the new project.
    Any assistance would be appreciated.

    My code for CellFormatting

        Private Sub DataGridView2_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles DataGridView2.CellFormatting

            If e.ColumnIndex = 0 Then
                e.CellElement.Font = RWLCompleteFont
                e.CellElement.ForeColor = Color.Black
                e.CellElement.NumberOfColors = 1
                e.CellElement.BackColor = Color.LightSteelBlue
                e.CellElement.DrawFill = True
                e.CellElement.DrawBorder = True
                e.CellElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.OuterInnerBorders
            End If
            If e.ColumnIndex = 1 Then
                e.CellElement.Font = RWLDetailsFont
                e.CellElement.ForeColor = Color.Black
                e.CellElement.NumberOfColors = 1
                e.CellElement.BackColor = Color.LightSkyBlue
                e.CellElement.DrawFill = True
                e.CellElement.DrawBorder = True
                e.CellElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.OuterInnerBorders
            End If

        End Sub

  2. Dimitar
    Admin
    Dimitar avatar
    1877 posts

    Posted 27 Sep Link to this post

    Hello Brent,

    RadGridView is using UI Virtualization. This means that cell elements are created only for the currently visible cells and they are reused when scrolling. This is why the styles are applied in other cells. So when using the formatting event you need to reset the styles of the cells. An example is available here: Formatting Cells | RadGridView.

    Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Brent
    Brent avatar
    4 posts
    Member since:
    Jun 2012

    Posted 29 Sep Link to this post

    I modified my code per below but I still get the same behaviour.

     

    Private Sub DataGridView2_CellFormatting(sender As Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles DataGridView2.CellFormatting
            If e.ColumnIndex = 0 Then
                e.CellElement.Font = RWLCompleteFont
                e.CellElement.ForeColor = Color.Black
                e.CellElement.NumberOfColors = 1
                e.CellElement.BackColor = Color.LightSteelBlue
                e.CellElement.DrawFill = True
                e.CellElement.DrawBorder = True
                e.CellElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.OuterInnerBorders
            Else
                e.CellElement.ResetValue(LightVisualElement.FontProperty, Telerik.WinControls.ValueResetFlags.Local)
            End If
            If e.ColumnIndex = 1 Then
                e.CellElement.Font = RWLDetailsFont
                e.CellElement.ForeColor = Color.Black
                e.CellElement.NumberOfColors = 1
                e.CellElement.BackColor = Color.LightSkyBlue
                e.CellElement.DrawFill = True
                e.CellElement.DrawBorder = True
                e.CellElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.OuterInnerBorders
            Else
                e.CellElement.ResetValue(LightVisualElement.FontProperty, Telerik.WinControls.ValueResetFlags.Local)
            End If
        End Sub

  4. Dimitar
    Admin
    Dimitar avatar
    1877 posts

    Posted 02 Oct Link to this post

    Hi, Brent,

    You need to reset all properties:
    Private Sub DataGridView2_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs)
        If e.ColumnIndex = 0 Then
            e.CellElement.Font = RWLCompleteFont
            e.CellElement.ForeColor = Color.Black
            e.CellElement.NumberOfColors = 1
            e.CellElement.BackColor = Color.LightSteelBlue
            e.CellElement.DrawFill = True
            e.CellElement.DrawBorder = True
            e.CellElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.OuterInnerBorders
        ElseIf e.ColumnIndex = 1 Then
            e.CellElement.Font = RWLDetailsFont
            e.CellElement.ForeColor = Color.Black
            e.CellElement.NumberOfColors = 1
            e.CellElement.BackColor = Color.LightSkyBlue
            e.CellElement.DrawFill = True
            e.CellElement.DrawBorder = True
            e.CellElement.BorderBoxStyle = Telerik.WinControls.BorderBoxStyle.OuterInnerBorders
        Else
            e.CellElement.ResetValue(LightVisualElement.FontProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.DrawBorderProperty, Telerik.WinControls.ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.BorderBoxStyleProperty, Telerik.WinControls.ValueResetFlags.Local)
     
        End If
    End Sub

    Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top