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

Add GridTemplateColumn to Radgrid programmatically

3 Answers 454 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Manuel Adum
Top achievements
Rank 1
Manuel Adum asked on 09 Nov 2012, 09:56 PM
Hi,
I have the following environment:  VS 2010, Asp.Net, Visual Basic and Telerik controls.

I started this project by trying to implement this solution with an unbound Radgrid, but then the Radgrid html code was not sent to the client in the page source file, I decided to use the bound Radgrid method.
Here is what i'm trying now:
I created an .aspx page that has an empty (no columns defined) Radgrid control bound to a datasource that I'm populating in the Onclick event code of a button using the following code:

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

            SqlDataSource1.SelectCommand = "exec CreateForecastEntry 'F2012v1', 'USA-ILMO-T1' "
            grdForecast.DataBind()

    End Sub


The datasource query returns a dataset with a variable number of columns (months) depending on some user settings.  I want the user to be able to edit the grid columns, so I want to add GridTemplateColumns with Textboxes for each of the columns.

The grid should look similar to this:

the Query returns in this case 3 columns, which I want to transform into 5 columns like this:

Column names:   Item | January | February | T_January | T_February
Column types:  GridBoundColumn | GridBoundColumn | GridBoundColumn | GridTemplateColumn (textbox) | GridTemplateColumn (textbox)

Note:  The number of months will be variable.

I have tried using the following code to create the columns, but it does not create the appropriate number of columns, and it creates them at the beginning of the grid:

Protected Sub grdForecast_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles grdForecast.ItemCreated
        Dim tc As New GridTemplateColumn

        tc.HeaderText = "qty_" & e.Item.UniqueID
        tc.UniqueName = "qty1_" & e.Item.UniqueID
        tc.ItemTemplate = New MyTemplate(tc.UniqueName)
        grdForecast.MasterTableView.Columns.Add(tc)

    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


This code will create a random number of GridTemplateColumns and I'm able to see them in the browser page source code, but I cannot access the textboxes content using this code:

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        Dim quantity As TextBox
        For Each row As GridDataItem In grdForecast.Items
             quantity = CType(row.FindControl("abc"), TextBox)
            quantity = Nothing
        Next
    End Sub

Any insight on what I'm doing wrong would be appreciated. Thanks
Eduardo

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Nov 2012, 04:22 AM
Hi,

When creating a grid dynamically that contains a template column, you must create the templates dynamically in the code-behind and assign them to the ItemTemplate properties of the column. To create a template dynamically, you must define a custom class that implements the ITemplate interface. Then you can assign an instance of this class to the ItemTemplate property of the GridTemplateColumn object. Check the following help documentation which explains more about this.
Programmatic Creation

Thanks,
Shinu.
0
Manuel Adum
Top achievements
Rank 1
answered on 12 Nov 2012, 03:00 PM
Hi Shinu,

Thanks for your post.  But if you read my post, you will see that I already did that and was able to add the textboxes, but I'm not able to later read their contents.

thanks,
Eduardo
0
Shinu
Top achievements
Rank 2
answered on 14 Nov 2012, 07:00 AM
Hi,

Here is the sample code that I tried to access the textbox from external button click.
VB:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
    For Each item As GridDataItem In RadGrid1.Items
        Dim txt As TextBox = DirectCast(item.FindControl("abc"), TextBox)
    Next
End Sub

Thanks,
Shinu.
Tags
Grid
Asked by
Manuel Adum
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Manuel Adum
Top achievements
Rank 1
Share this question
or