TelerikDeveloper.com

TelerikDeveloper.com

sgray128 9/21/2011 8:30:40 AM

Radgrid ItemDataBound

This article provides a code example of how to use the Telerik RadGrid ItemDataBound event


 

Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    Dim oRow As DataRowView
 
    If TypeOf e.Item Is Telerik.Web.UI.GridDataItem Then
 
        'this gives us access to the data side of things
        oRow = CType(e.Item.DataItem, DataRowView)
        Dim strStatus As String = oRow("status")
        Dim intID As Int32 = oRow("ID")
 
        'get a reference to a TEMPLATED control
        Dim hypSubject As HyperLink = CType(e.Item.FindControl("hypSubject"), HyperLink)
        hypSubject.Text = oRow("subject")
 
        'use business logic on the control
        If strStatus = "Article" Then
            hypSubject.NavigateUrl = "~/Article.aspx?ArticleID=" & intID
        Else
            hypSubject.NavigateUrl = "~/Thread.aspx?threadid=" & intID
        End If
 
        'get a reference to a GridBoundColumn
        Dim oItem As GridDataItem = DirectCast(e.Item, GridDataItem)
        oItem("itemdesc").ForeColor = Drawing.Color.Red
        oItem("itemdesc").Font.Bold = True
 
    End If
 
    'used to reference the group headers
    If TypeOf e.Item Is GridGroupHeaderItem Then
        oRow = CType(e.Item.DataItem, DataRowView)
        Dim bLocked As Boolean = oRow("Locked")
        If bLocked Then
            Dim btnReceive As RadButton = CType(e.Item.FindControl("btnReceive"), RadButton)
            btnReceive.Visible = False
        End If
 
    End If
 
End Sub

This example shows how to get a reference to a grid bound column. The uniquename for the column is "imgWarning". To get the type to convert it to, run the code below and you'll get a convert error on the 'Dim imgWarning' line. The error will give you the type to convert it to.

 

Private Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is Telerik.Web.UI.GridDataItem Then
        Dim item As GridDataItem = e.Item
 
        'this gives us access to the data side of things
        Dim oRow As DataRowView = CType(e.Item.DataItem, DataRowView)
        Dim warning As Int16 = oRow("warning")
 
        Dim imgWarning As System.Web.UI.WebControls.Image = item("imgWarning").Controls(0)
 
        If warning = 1 Then
            imgWarning.ImageUrl = "~/images/warning.png"
            Session("warning") = "Lines exist that exceed available quantity"
        Else
            imgWarning.ImageUrl = "~/images/white.png"
        End If
 
        Me.lblWarning.Text = Session("warning")
    End If
 
End Sub