Programmatically Changing Templated Columns at Runtime

Thread is closed for posting
2 posts, 0 answers
  1. 3816C4DC-A3E9-4644-9D7D-5C5FE66846E3
    3816C4DC-A3E9-4644-9D7D-5C5FE66846E3 avatar
    20 posts
    Member since:
    Jul 2012

    Posted 28 Nov 2006 Link to this post

    Requirements

    r.a.d.controls version

    Q3 2006 (RAD GRID)

    .NET version

    2.0.50727

    Visual Studio version

    2005

    programming language

    VB.NET

    browser support

    all browsers supported by r.a.d.controls


     
    PROJECT DESCRIPTION
    It is sometimes necessary to swap raw grid cell data for something else - the template column functionality that comes built into r.a.d. grid enables you to take the first step to achieving this.

    But simplying having a templated column on its own is not enough - there are many occassions were it is necessary to have some sort of data-bound functionality associated with the templated-column or render out the templated-column based on underlying raw data.

    In this example, we demonstrate a simple message system using the r.a.d. grid - its purpose is to simply display messages to the end-user.
    NB: The actual functionality for this system has been stripped out as it is sensitive to client-projects.

    In the message system we use a simple table in Access 2003 to store our messages using the following schema:

    • ID
    • CreatedOn
    • From
    • Subject
    • IsRead

    When rendered onto the grid, we do not want to dislay the ID or IsRead columns from the schema - however we want to add in two templated-columns - Select and Status. For the purposes of this code-sample, we ignore the Select templated-column.

    The Status templated-column simply contains the Image Control, supplied within the .NET System.Web DLL/namespace.

    Instead of displaying the checkbox/boolean column IsRead, we are going to use the Status columns' Image control to render one of two possible images depending on the value contained in the IsRead column during the databinding event cycle - as with all messages, we have two possible image states, read (TRUE) and unread (FALSE).

    Next we simply handle the r.a.d. grids' ItemDataBound event in the code-behind file:

        Protected Sub RadGrid1_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemDataBound  
            'Declare variables for use and default them to null  
            Dim dataItem As GridDataItem = Nothing 
            Dim data As Data.DataRowView = Nothing 
            Dim img As Image = Nothing 
            Dim value As Boolean = Nothing 
            Try 
                'Check that we are potentially referencing a  
                ' GridDataItem object:  
                If TypeOf e.Item Is GridDataItem Then 
     
                    'Using the event agruments, we obtain a reference to the  
                    ' current Grid DataItem:  
                    dataItem = CType(e.Item, GridDataItem)  
     
                    'From the DataItem we get a reference to the databinding object  
                    'which is a DataRowView object:  
                    data = CType(dataItem.DataItem, Data.DataRowView)  
     
                    'Next, we get a reference to the Image control contained  
                    'within the current Grid DataItem:  
                    img = CType(dataItem("Status").Controls(1), Image)  
     
                    'Last, but not least, we find out the value  
                    ' of the IsRead row item of type boolean:  
                    value = CType(data.Row.Item("IsRead"), Boolean)  
     
                    'IF the value is TRUE then use the Read image url and tooltip,  
                    'OTHERWISE, use the Unread image url and tooltip:  
                    If value Then 
                        img.ImageUrl = "~/res/pics/MailItem_Read_16.png" 
                        img.ToolTip = "This message has been read." 
                    Else 
                        img.ImageUrl = "~/res/pics/MailItem_Unread_16.png" 
                        img.ToolTip = "This message has not been read." 
                    End If 
     
                End If 
            Catch ex As Exception  
                'The exception is ignored  
            Finally 
                'Finally, destroy used variables  
                dataItem = Nothing 
                data = Nothing 
                img = Nothing 
                value = Nothing 
            End Try 
        End Sub 


    Now we have a templated-column that can be custom-rendered with whatever we want!

    Hope this is of help to someone.

  2. C7498A83-7E2E-418C-8791-93EF573A7569
    C7498A83-7E2E-418C-8791-93EF573A7569 avatar
    9934 posts
    Member since:
    Nov 2016

    Posted 01 Dec 2006 Link to this post

    Hello Mark,

    Thank you for the description and the sample project - it can be indeed helpful for people which are searching similar functionality for template column. I have added 1000 telerik points to your account for the involvement - thank you for the time you dedicated to assemble the demo.

    And keep posting ;)

    Best regards,

    Stephen

    the telerik team

Back to Top

This Code Library is part of the product documentation and subject to the respective product license agreement.