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

How to reuse grid template columns

2 Answers 292 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michel
Top achievements
Rank 1
Michel asked on 03 Dec 2009, 03:04 PM
I have a number of grids that partly share the same structure. For example, many grids have 'Id', 'Description', 'DisplayOrder', 'LastUpdateDate' and 'LastUpdateUserId' columns that are identical. The GridTemplateColumn for these common columns are all set declaratively the same in the different grids.

I would like to know what is the best approach in reusing some code.

For example, I considered creating a web user control for each column, but this did not work (as it seems that the telerik:GridTemplateColumn tag was not recognized in the ascx file and also that the control in the columns section has to be a 'Telerik.Web.UI.GridColumn').

Thanks.

2 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 08 Dec 2009, 03:14 PM
Hello Michel,

There are few examples that utilize GridTemplateColumn for reusing the code. You should be able to do the same with the one that you are using. Please check the links for reference:
http://www.telerik.com/help/aspnet-ajax/grdimplementingfilteringfortemplatecolumns.html
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/filteringtemplatecolumns/defaultcs.aspx

Best wishes,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Michel
Top achievements
Rank 1
answered on 09 Dec 2009, 06:20 PM

Thanks for the tip Nikolay.

Using web custom controls will probably do the trick.

It took a while to get it to work properly and I had to research defining template programmatically, but everything now seems ok. Let me share my code for others that would like to do the same.

I used to have this.

<telerik:RadGrid
    ID="SeasonCategoriesGrid"    
    runat="server"    
    (...)>    
    <MasterTableView      
        (...)>    
        <Columns>    
            <telerik:GridTemplateColumn      
                DataField="Description"    
                FilterControlWidth="115px"      
                HeaderStyle-Width="160px"      
                HeaderText="<%$ Resources:Global, Description%>"      
                SortExpression="Description">     
                <ItemTemplate>    
                    <%# Eval("Description") %>    
                </ItemTemplate>    
                <EditItemTemplate>    
                    <wcc:TextInput      
                        ID="DescriptionInput"      
                        runat="server"      
                        IsRequired="true"      
                        MaxLength="25"    
                        Text='<%# Bind("Description") %>' />    
                </EditItemTemplate>    
            </telerik:GridTemplateColumn>    
                 
            (...)     
                 
        </Columns>    
    </MasterTableView>    
</telerik:RadGrid> 

And I now have this.

<telerik:RadGrid  
    ID="SeasonCategoriesGrid"    
    runat="server"    
    (...)>    
    <MasterTableView      
        (...)>    
        <Columns>    
            <wcc:DescriptionGridTemplateColumn />    
             
            (...)     
                 
        </Columns>    
    </MasterTableView>    
</telerik:RadGrid> 

 

Namespace MyWebCustomControls        
       
    Public Class DescriptionGridTemplateColumn        
        Inherits Telerik.Web.UI.GridTemplateColumn        
                
        Public Sub New()        
            Me.ItemTemplate = New DescriptionItemTemplate()        
            Me.EditItemTemplate = New DescriptionEditItemTemplate()        
       
            Me.DataField = "Description"       
            Me.FilterControlWidth = New Unit(115, UnitType.Pixel)        
            Me.HeaderStyle.Width = New Unit(160, UnitType.Pixel)        
            Me.HeaderText = Resources.Global.Description        
            Me.SortExpression = "Description"       
        End Sub       
       
        Private Class DescriptionItemTemplate        
            Implements IBindableTemplate        
       
            Private _description As WebControls.Literal        
       
            Public Sub InstantiateIn(ByVal container As Control) Implements IBindableTemplate.InstantiateIn        
                _description = New WebControls.Literal()        
                AddHandler _description.DataBinding, AddressOf Me.Description_Databinding        
                container.Controls.Add(_description)        
            End Sub       
       
            Public Sub Description_Databinding(ByVal sender As ObjectByVal e As EventArgs)        
                Dim dataItem As Telerik.Web.UI.GridDataItem = DirectCast(CType(sender, Control).NamingContainer, Telerik.Web.UI.GridDataItem)        
                CType(sender, WebControls.Literal).Text = CStr(DataBinder.Eval(dataItem.DataItem, "Description"))        
            End Sub       
       
            ' It seems that in order to perform deletes, the ItemTemplane needs to   
            ' not only implement ITemplate, but also IBindable Template.   
            ' Therefore the ExtractValues method needs to be implemented, but I don't beleive   
            ' that the returned data is really used for anything.      
            Public Function ExtractValues(ByVal container As System.Web.UI.Control) As System.Collections.Specialized.IOrderedDictionary Implements IBindableTemplate.ExtractValues        
                Return New Collections.Specialized.OrderedDictionary()        
            End Function       
       
        End Class       
       
        Private Class DescriptionEditItemTemplate        
            Implements IBindableTemplate        
       
            Private _descriptionInput As MyWebCustomControls.TextInput        
       
            Public Sub InstantiateIn(ByVal container As Control) Implements IBindableTemplate.InstantiateIn        
                _descriptionInput = New MyWebCustomControls.TextInput()        
                _descriptionInput.ID = "DescriptionInput"       
                _descriptionInput.IsRequired = "true"       
                _descriptionInput.MaxLength = "25"       
                AddHandler _descriptionInput.DataBinding, AddressOf Me.DescriptionInput_Databinding        
                container.Controls.Add(_descriptionInput)        
            End Sub       
       
            Public Sub DescriptionInput_Databinding(ByVal sender As ObjectByVal e As EventArgs)        
                Dim dataItem As Telerik.Web.UI.GridEditFormItem = DirectCast(CType(sender, Control).NamingContainer, Telerik.Web.UI.GridEditFormItem)        
                CType(sender, MyWebCustomControls.TextInput).Text = TryCast(DataBinder.Eval(dataItem.DataItem, "Description"), String)        
            End Sub       
       
            Public Function ExtractValues(ByVal container As Control) As Collections.Specialized.IOrderedDictionary Implements IBindableTemplate.ExtractValues        
                Dim returnDictionary As Collections.Specialized.OrderedDictionary = New Collections.Specialized.OrderedDictionary()        
                returnDictionary.Add("Description", (DirectCast(((DirectCast(container, Telerik.Web.UI.GridEditFormItem)).FindControl("DescriptionInput")), MyWebCustomControls.TextInput)).Text)        
                Return returnDictionary        
            End Function       
       
        End Class       
       
    End Class       
       
End Namespace 

(Note that "wcc" refers to my custom controls in the MyWebCustomControls namespace.)

As you can see, instead of specifying the ItemTemplate, ItemEditTemplate and properties declaratively for my description GridTemplateColumn, this can now be replaced by a single line.

It does take a little work to set up the web customs classes for each of my columns, but given that these are repeated in a few grids, I have less lines of code. In addition I gain all the benefits of code reuse.

Tags
Grid
Asked by
Michel
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Michel
Top achievements
Rank 1
Share this question
or