CODE: Power of ViewCellFormatting

2 posts, 0 answers
  1. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 28 Jul 2011 Link to this post

    I thought I would contribute this proof of concept code I had to work out.  A single cell in a grid needed to update data via a call to a remote system.  However, if the grid had thousands of records, I did not want it to make thousands of remote calls on load.  So, I looked at the ViewCellFormatting event which allows me to retrieve the data only when the cell is visible.  

    I also "cached" the data in the cell by setting its flag to "UPDATED".  The "Refresh Data" function actually loops through all rows, clears the cells' tags, and sets the value to zero.  When the visible cell's value is changed, the ViewCellFormatting event is fired and the remote call is made again.

    In this proof of concept code, I am simply just updating the row to the current time's Second.  The first button refreshes the data and makes the other non-visible cell's stale by removing the tag.  

    The second button will show the "i" count to prove the "remote" call is called only on the visible cells.

    I tip my hat to Telerik for this pretty powerful and useful functionality!

    Imports Telerik.WinControls.UI
    Public Class Form1
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            i = 0
            For Each oRow As GridViewDataRowInfo In RadGridView1.Rows()
                oRow.Cells(0).Tag = ""
                oRow.Cells(0).Value = 0
        End Sub
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim list As New ArrayList()
            Dim i As Integer = 0
            While i < 55
                list.Add(New ValueType(Of Integer)((i + 1)))
                '         System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
                i += 1
            End While
            Me.RadGridView1.DataSource = list
        End Sub
        Private Sub CreateFormControls()
            Me.RadGridView1 = New Telerik.WinControls.UI.RadGridView()
            Me.Button1 = New System.Windows.Forms.Button()
            Me.Button2 = New System.Windows.Forms.Button()
            Me.RadGridView1.Location = New System.Drawing.Point(13, 13)
            Me.RadGridView1.Name = "RadGridView1"
            Me.RadGridView1.Size = New System.Drawing.Size(267, 194)
            Me.RadGridView1.TabIndex = 0
            Me.RadGridView1.Text = "RadGridView1"
            Me.Button1.Location = New System.Drawing.Point(13, 213)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(252, 23)
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Refresh Viewable Cells...Make other cell stale"
            Me.Button1.UseVisualStyleBackColor = True
            Me.Button2.Location = New System.Drawing.Point(12, 242)
            Me.Button2.Name = "Button2"
            Me.Button2.Size = New System.Drawing.Size(136, 23)
            Me.Button2.TabIndex = 1
            Me.Button2.Text = "Total Data Updates"
            Me.Button2.UseVisualStyleBackColor = True
        End Sub
        Public Class ValueType(Of T)
            Private item As T
            Public Sub New()
            End Sub
            Public Sub New(ByVal item As T)
                Me.item = item
            End Sub
            Public Property ItemProperty() As T
                    Return Me.item
                End Get
                Set(ByVal value As T)
                    Me.item = value
                End Set
            End Property
        End Class
        Private Sub RadGridView1_ViewCellFormatting(sender As Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.ViewCellFormatting
            If e.ColumnIndex = 0 Then
                If e.Row.Cells(0).Tag <> "UPDATED" Then
                    e.CellElement.Value = Now.Second
                    e.Row.Cells(0).Tag = "UPDATED"
                    i += 1
                End If
            End If
        End Sub
        Dim i As Integer
        Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
            MsgBox("Date Update Called " & i & " times")
        End Sub
    End Class

  2. Stefan
    Stefan avatar
    2891 posts

    Posted 02 Aug 2011 Link to this post

    Hello Jason,

    Thank you for sharing your code with the community, I am sure that someone might benefit from it. Just to let you know, we do have a Code Library section, which is intended to be used by our community for code sharing. Next time, feed free to add your code there.

    I have updated your Telerik points for the community effort.
    Kind regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. DevCraft R3 2016 release webinar banner
Back to Top