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

Data binding dynamic GridTemplateColumn

1 Answer 261 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pierre
Top achievements
Rank 2
Iron
Iron
Pierre asked on 08 Feb 2008, 09:23 PM
HI, I have a Prometheus RadGrid that I build dynamically when a user click on a button. With the Prometheus Ajax, I clear all the colunm definition (depending on the button clicked the colunm change) and recreate it. On one situation, I have a template column that display a icon or a blank icon dépending on boolean value. All work good, the first time the grid are created the column header template and the item template work good. But if I click on one item in the grid, the rad ajax update some textbox, but I lost the header and item template.

Any suggestion?

The CreeGridAct are called after a ajaxified postback button click


Create the grid and template dynamically:
 Private Sub CreeGridAct()  
        Dim column1 As GridBoundColumn  
        Dim ColumnTmpl As GridTemplateColumn  
 
        EffaceChamps()  
 
        RadGridUsager.Skin = "WebBlue" 
        RadGridUsager.Width = Unit.Pixel(450)  
        RadGridUsager.PageSize = 15 
        RadGridUsager.AllowPaging = True 
        RadGridUsager.AllowSorting = True 
        RadGridUsager.GridLines = GridLines.Horizontal  
        RadGridUsager.AllowMultiRowSelection = True 
        RadGridUsager.PagerStyle.Mode = GridPagerMode.NextPrevNumericAndAdvanced  
        RadGridUsager.AutoGenerateColumns = False 
        RadGridUsager.GroupingEnabled = False 
        RadGridUsager.ShowGroupPanel = False 
        RadGridUsager.ShowFooter = True 
 
        RadGridUsager.ClientSettings.AllowDragToGroup = False 
        RadGridUsager.ClientSettings.AllowColumnsReorder = True 
        RadGridUsager.ClientSettings.EnablePostBackOnRowClick = True 
        RadGridUsager.ClientSettings.ApplyStylesOnClient = True 
        RadGridUsager.ClientSettings.Selecting.AllowRowSelect = True 
 
        RadGridUsager.MasterTableView.PageSize = 15 
        RadGridUsager.MasterTableView.Width = Unit.Percentage(100)  
 
        RadGridUsager.MasterTableView.Columns.Clear()  
 
        'Creation des colones pour cet affichage  
        column1 = New GridBoundColumn  
        RadGridUsager.MasterTableView.Columns.Add(column1)  
        column1.DataField = "Usager" 
        column1.HeaderText = "Usager" 
        column1.Visible = False 
 
        column1 = New GridBoundColumn  
        RadGridUsager.MasterTableView.Columns.Add(column1)  
        column1.DataField = "Nom" 
        column1.HeaderText = "Nom" 
 
        column1 = New GridBoundColumn  
        RadGridUsager.MasterTableView.Columns.Add(column1)  
        column1.DataField = "Compagnie" 
        column1.HeaderText = "Compagnie" 
 
        column1 = New GridBoundColumn  
        RadGridUsager.MasterTableView.Columns.Add(column1)  
        column1.DataField = "Ville" 
        column1.HeaderText = "Ville" 
 
        column1 = New GridBoundColumn  
        RadGridUsager.MasterTableView.Columns.Add(column1)  
        column1.DataField = "Telephone" 
        column1.HeaderText = "Téléphone" 
 
        ColumnTmpl = New GridTemplateColumn  
        RadGridUsager.MasterTableView.Columns.Add(ColumnTmpl)  
        ColumnTmpl.UniqueName = "Suspendu" 
        ColumnTmpl.Groupable = False 
        ColumnTmpl.SortExpression = "Suspendu" 
        ColumnTmpl.HeaderStyle.Width = 20 
        ColumnTmpl.HeaderTemplate = New SusHeaderTemplate  
        ColumnTmpl.ItemTemplate = New SusItemTemplate  
 
 
        RadGridUsager.Rebind()  
 
    End Sub  
 
    Private Class SusHeaderTemplate  
        Implements ITemplate  
 
        Public Sub New()  
            MyBase.New()  
        End Sub  
 
        Protected imgSus As Image  
 
        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn  
            imgSus = New Image  
            imgSus.ImageUrl = "images/Misc/suspendu.gif" 
            container.Controls.Add(imgSus)  
        End Sub  
    End Class  
 
    Private Class SusItemTemplate  
        Implements ITemplate  
 
        Public Sub New()  
            MyBase.New()  
        End Sub  
 
        Protected imgSus As Image  
 
        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn  
            imgSus = New Image  
            imgSus.ImageUrl = "images/Misc/Blank.gif" 
            container.Controls.Add(imgSus)  
 
            AddHandler imgSus.DataBinding, AddressOf Me.imgSus_DataBinding  
        End Sub  
 
        Private Sub imgSus_DataBinding(ByVal sender As Object, ByVal e As EventArgs)  
            Dim imgSus As Image = CType(sender, Image)  
            Dim container As GridDataItem = CType(imgSus.NamingContainer, GridDataItem)  
 
            If Convert.ToBoolean(DataBinder.Eval(container.DataItem, "Suspendu")) = True Then  
                imgSus.ImageUrl = "images/Misc/suspendu.gif" 
            Else  
                imgSus.ImageUrl = "images/Misc/blank.gif" 
            End If  
        End Sub  
    End Class 

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 11 Feb 2008, 04:55 PM
Hello Pierre,

In order templated columns (which are added programmatically) to persist their state properly you should create the entire RadGrid control in the code-behind inside the page's OnInit handler. Please refer to this article from the online documentation for more information on this subject.

Best regards,
Rosen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Pierre
Top achievements
Rank 2
Iron
Iron
Answers by
Rosen
Telerik team
Share this question
or