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

Conditional Formatting in Radgridview

3 Answers 269 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Brent
Top achievements
Rank 1
Brent asked on 26 Sep 2017, 01:58 PM

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

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 27 Sep 2017, 05:22 AM
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.
0
Brent
Top achievements
Rank 1
answered on 29 Sep 2017, 07:05 PM

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

0
Dimitar
Telerik team
answered on 02 Oct 2017, 08:26 AM
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.
Tags
General Discussions
Asked by
Brent
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Brent
Top achievements
Rank 1
Share this question
or