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

Programmatically Created GridTemplateColumn Values Disappear After Paging

1 Answer 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jesse
Top achievements
Rank 1
Jesse asked on 27 Feb 2009, 03:35 PM
Hello,

I have thoroughly looked through the online help for how to create GridTemplateColumns programmatically and have been successful in doing so.  My whole goal is to have a GridTemplateColumn whose ItemTemplate just shows a value (Employee Name), and whose EditTemplate has a RadComboBox with a MultiColumnDropdown.  I assumed I had to use a template column for this because I didn't see any way to do this with a GridDropDownColumn.  

My grid is tied to an ObjectDataSource that is tied to a DataTable stored as a session variable.

Initially, the data loads fine and everything looks good.  Unfortunately, when I advance to another grid page after the inital load of the web form, the values in the GridTemplateColumn disappear while the other non-template bound columns correctly display data for the next page.

I notice that when I hookup the DataBinding event on the InstantiateIn method, it is called for each row in the page on the initial load of the form.  This is NOT called again after moving to another grid page.  Maybe the trick here is to get that event to fire on a page change, or maybe it's something else? Anyway, here is the code how I'm generating the column.  I have tried to move the loading of the grid into the Page_Init function instead of the Page_Load, but to no avail.  Your help is greatly appreciated!

This method "remakes" an already created bound column into a GridTemplateColumn.  I do this so I can call it generically all over our application.  NOTE that I only have the ItemTemplate at the moment.
Public Sub SetMutliColumnDropDown(ByVal ColumnName As String, ByVal dt As DataTable, ByVal HeaderText As String, ByVal ValueField As String, ByVal TextField As String, ByVal HiddenColumns As String) 
        Session("dd" & ColumnName) = dt 
        Dim ColIndex As Integer = RadGrid1.MasterTableView.Columns.IndexOf(RadGrid1.MasterTableView.Columns.FindByUniqueName(ColumnName)) 
        RadGrid1.MasterTableView.Columns.Remove(RadGrid1.MasterTableView.Columns.FindByUniqueName(ColumnName)) 
 
        Dim col As New GridTemplateColumn() 
        RadGrid1.MasterTableView.Columns.AddAt(ColIndex, col) 
        col.ItemTemplate = New MultiColumnDropDownItemTemplate(dt, ValueField, TextField, HiddenColumns) 
        col.UniqueName = ValueField 
    End Sub 


ItemTemplate class.  
Public Class MultiColumnDropDownItemTemplate : Implements ITemplate 
    Private dt As DataTable 
    Private ValueField As String 
    Private TextField As String 
    Private HiddenColumns As String 
 
    Public Sub New(ByVal dt As DataTable, ByVal ValueField As String, ByVal TextField As String, ByVal HiddenColumns As String) 
        Me.dt = dt 
        Me.ValueField = ValueField 
        Me.TextField = TextField 
        Me.HiddenColumns = HiddenColumns 
    End Sub 
 
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn 
        Dim lc As New Literal 
        lc.Text = ValueField 
        AddHandler lc.DataBinding, AddressOf TemplateControl_DataBinding 
        container.Controls.Add(lc) 
    End Sub 
 
    Public Sub TemplateControl_DataBinding(ByVal sender As Object, ByVal e As EventArgs) 
        Dim lc As Literal = CType(sender, Literal) 
        Dim container As GridDataItem = lc.NamingContainer 
        Dim pk As Object = DataBinder.Eval(container.DataItem, lc.Text) 
        For Each row As DataRow In dt.Rows 
            If row(ValueField).ToString = pk.ToString Then 
                lc.Text = row(TextField) 
                Exit For 
            End If 
        Next 
    End Sub 
End Class 

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 03 Mar 2009, 09:48 AM
Hi Jesse,

Please, make sure the control is created in the PageInit event handler, as shown in the following article.
This is especially important for template columns.
I hope this gets you started properly.

Kind regards,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Jesse
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or