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

Adding Template columns dynamically in Radgrid

2 Answers 754 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kaushik
Top achievements
Rank 1
Kaushik asked on 03 Dec 2013, 04:22 PM

I am trying to add template columns dynamically to a grid which already has two columns

I used the code 
Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
 
Dim table As New DataTable()
table.Load(get data from database)
 
For rowCounter As Integer = 0 To table.Rows.Count - 1
 
For columnCounter As Integer = 0 To table.Columns.Count Step -1
 
Dim tc As New GridTemplateColumn
tc.HeaderText = "qty_1"
tc.UniqueName = "qty1" & columnCounter.ToString
tc.ItemTemplate = New MyTemplate(tc.UniqueName)
grdSchedule.MasterTableView.Columns.Add(tc)
 
next
next
 
End Sub
 
Private Class MyTemplate
        Implements ITemplate
 
        Protected textBox As TextBox
        Private colname As String
 
        Public Sub New(ByVal cName As String)
            MyBase.New()
            colname = cName
        End Sub
 
        Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
            textBox = New TextBox
            textBox.ID = "abc"
            container.Controls.Add(textBox)
        End Sub
    End Class

I am able to create columns in the page Init event. But I have a problem using that logic in Init event, as I need to get data which decides the number of columns that need to be created, from database. I want to use it in pageload event.

Can you please help me in this. I am not able to create columns dynamically in pageload event now.

I followed the below posts but all of them are talking about page init event.
http://www.telerik.com/community/forums/aspnet-ajax/grid/problem-creating-radgrid-gridtemplatecolumn-dynamically.aspx
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html


Thanks,
Kaushik

2 Answers, 1 is accepted

Sort by
0
Kaushik
Top achievements
Rank 1
answered on 04 Dec 2013, 07:17 AM

Can you please help me in this.
0
Princy
Top achievements
Rank 2
answered on 04 Dec 2013, 10:09 AM
Hi Kaushik,

When creating template columns programmatically, the grid must be generated completely in the code-behind using the Page_Init event, so that the template controls can be added to the ViewState. Its advisable to do it in Page_Init event. Below is a sample code snippet that i tried creating template columns in Page_Load, please try it and see if it helps.

ASPX:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

VB:
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
 Dim RadGrid1 As New RadGrid()
 RadGrid1.AutoGenerateColumns = False
 RadGrid1.DataSourceID = "SqlDataSource1"
 RadGrid1.AllowPaging = True
 RadGrid1.PageSize = 4
 Dim templateColumnName As String = "ContactName"
 Dim templateColumn As New GridTemplateColumn()
 templateColumn.ItemTemplate = New MyTemplate(templateColumnName)
 templateColumn.HeaderText = templateColumnName
 Dim boundColumn1 As New GridBoundColumn()
 boundColumn1.DataField = "ContactName"
 boundColumn1.UniqueName = "ConactName"
 boundColumn1.HeaderText = "Bound Column"
 RadGrid1.MasterTableView.Columns.Add(templateColumn)
 RadGrid1.MasterTableView.Columns.Add(boundColumn1)
 PlaceHolder1.Controls.Add(RadGrid1)
 RadGrid1.Rebind()
End Sub
 
 Private Class MyTemplate
 Implements ITemplate
 Protected lControl As LiteralControl
 Protected textBox As TextBox
 
 Private colname As String
 Public Sub New(cName As String)
     colname = cName
 End Sub
 Public Sub InstantiateIn(container As System.Web.UI.Control)
     lControl = New LiteralControl()
     lControl.ID = "lControl"
     textBox = New TextBox()
     textBox.ID = "templateColumnTextBox"
     container.Controls.Add(lControl)
     container.Controls.Add(textBox)
     container.Controls.Add(New LiteralControl("<br />"))
 End Sub
 End Class

Thanks,
Princy
Tags
Grid
Asked by
Kaushik
Top achievements
Rank 1
Answers by
Kaushik
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or