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

using cellformatting event to color cells fails on grouping

3 Answers 65 Views
GridView
This is a migrated thread and some comments may be shown as answers.
dd
Top achievements
Rank 1
dd asked on 02 Mar 2018, 10:08 PM

why is the grid coloring wrong cells when i group the data? it works until you do a group by.

 

Imports System.ComponentModel
Imports Telerik.WinControls
Imports Telerik.WinControls.UI

Public Class Form1
    Private _list As New BindingList(Of data)
    Private _groups() As String = {"one", "two", "three", "four"}
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        GenData()
        grid.DataSource = _list

    End Sub

    Private Sub GenData()
        For i = 0 To 1000
            _list.Add(New data() With {.Foo = "foo" + i.ToString,
                                     .Bar = _groups(i Mod 4),
                                     .Bye = "bye" + i.ToString,
                                     .Hi = "hi" + i.ToString,
                                     .Index = i})
        Next
    End Sub

    Private Sub grid_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles grid.CellFormatting
        If TypeOf e.Row Is GridViewDataRowInfo Then
            If e.CellElement.ColumnInfo.Name.ToLower = "foo" Then
                Dim b As data = CType(e.Row.DataBoundItem, data)
                If b.Index Mod 2 > 0 Then
                    e.CellElement.DrawFill = True
                    e.CellElement.BackColor = Color.Pink
                    Console.WriteLine(e.CellElement.ColumnInfo.Name + " going pink - " + e.CellElement.RowIndex.ToString)
                    e.CellElement.GradientStyle = GradientStyles.Linear
                Else
                    e.CellElement.DrawFill = False
                    e.CellElement.BackColor = Color.White
                    Console.WriteLine(e.CellElement.ColumnInfo.Name + " going white - " + e.CellElement.RowIndex.ToString)
                    e.CellElement.GradientStyle = GradientStyles.Solid
                End If
            End If
        End If
    End Sub

End Class

Public Class data

    Public Property Foo As String = ""
    Public Property Bar As String = ""
    Public Property Hi As String = ""
    Public Property Bye As String = ""
    Public Property Index As Integer = -1
End Class

3 Answers, 1 is accepted

Sort by
0
dd
Top achievements
Rank 1
answered on 02 Mar 2018, 10:48 PM
version 2018.1.116.40
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Mar 2018, 02:38 PM
Hello,

Thank you for writing.  

This ia standard result when the formatting is not properly applied. Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse), all customization should be reset for the rest of the cell elements. According to the provided code snippet you have several 'if' clauses. Make sure that you reset the applied settings for all 'else' cases. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/cells/formatting-cells

I hope this information helps. Should you have further questions I would be glad to help. 
 
 Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
dd
Top achievements
Rank 1
answered on 06 Mar 2018, 10:54 PM

great! thanks, here is my cellformatting event handler now. this works:

Private Sub grid_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles grid.CellFormatting
    If TypeOf e.Row Is GridViewDataRowInfo Then
        If e.CellElement.ColumnInfo.Name.ToLower = "foo" Then
            Dim b As data = CType(e.Row.DataBoundItem, data)
            If b.Index Mod 2 > 0 Then
                e.CellElement.DrawFill = True
                e.CellElement.BackColor = Color.Pink
                Console.WriteLine(e.CellElement.ColumnInfo.Name + " going pink - " + e.CellElement.RowIndex.ToString)
                e.CellElement.GradientStyle = GradientStyles.Linear
            Else
                e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
                e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
                e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
            End If
        Else
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
        End If
 
    End If
End Sub
Tags
GridView
Asked by
dd
Top achievements
Rank 1
Answers by
dd
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or