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

CODE: Power of ViewCellFormatting

1 Answer 60 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jason Parrish
Top achievements
Rank 1
Jason Parrish asked on 28 Jul 2011, 05:41 PM
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
        Next
 
    End Sub
 
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CreateFormControls()
 
        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()
        '
        'RadGridView1
        '
        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"
        '
        'Button1
        '
        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
        '
        'Button2
        '
        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
        '
        'Form1
        '
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.RadGridView1)
 
 
    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
            Get
                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

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 02 Aug 2011, 12:57 PM
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,
Stefan
the Telerik team

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

Tags
GridView
Asked by
Jason Parrish
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or