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