I am creating dynamic columns in a grid. How do I do this in the code behind:
<telerik:GridTemplateColumn UniqueName="vendor" HeaderText="vendor">
<ItemTemplate><%# Eval("vendor") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="vendorDropDown" runat="server">
<asp:ListItem Text="Al1" Value="Al1"/>
<asp:ListItem Text="Al2" Value="Al2"/>
<asp:ListItem Text="Al3" Value="Al3"/>
<asp:ListItem Text="Al4" Value="Al4"/>
</asp:DropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
14 Answers, 1 is accepted

Check the following help documentation which explains the same.
Programmatic Creation
-Shinu.


Here is the sample class definition for TemplateColumn.
C#:
public
partial
class
TemplateColumn : ITemplate
{
public
void
InstantiateIn(Control container)
{
CheckBox box =
new
CheckBox();
box.ID =
"boxControl"
;
box.AutoPostBack =
true
;
container.Controls.Add(box);
Button btn1 =
new
Button();
btn1.ID =
"btn1"
;
btn1.Click +=
new
EventHandler(btn1_Click);
btn1.Text =
"click"
;
container.Controls.Add(btn1);
}
}
-Shinu.

<telerik:GridTemplateColumn UniqueName="vendor" HeaderText="vendor">
<ItemTemplate><%# Eval("vendor") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="vendorDropDown" runat="server">
<asp:ListItem Text="Al1" Value="Al1"/>
<asp:ListItem Text="Al2" Value="Al2"/>
<asp:ListItem Text="Al3" Value="Al3"/>
<asp:ListItem Text="Al4" Value="Al4"/>
</asp:DropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
i will better understand

In order to achieve your goal you should use the approach from the last section of this help article. However, the example shown in this help topic is using only ItemTemplate. In case you need to use ItemTemplate and EditItemTemplate, you should declare additional class similar to the one that is used for ItemTemplate.
But instead of the controls that are shown in the help you should put the DropDownList control in it. Like this:
private
class
MyEditTemplate : ITemplate
{
protected
DropDownList ddl;
private
string
colname;
public
MyEditTemplate(
string
cName)
{
colname = cName;
}
public
void
InstantiateIn(System.Web.UI.Control container)
{
ddl =
new
DropDownList();
ListItem item1 =
new
ListItem(
"Al1"
,
"Al1"
);
ListItem item2 =
new
ListItem(
"Al2"
,
"Al2"
);
ListItem item3 =
new
ListItem(
"Al3"
,
"Al3"
);
ListItem item4 =
new
ListItem(
"Al4"
,
"Al4"
);
Table table =
new
Table();
TableRow row1 =
new
TableRow();
TableCell cell11 =
new
TableCell();
TableCell cell12 =
new
TableCell();
row1.Cells.Add(cell11);
row1.Cells.Add(cell12);
table.Rows.Add(row1);
cell11.Text = colname +
": "
;
cell12.Controls.Add(ddl);
container.Controls.Add(table);
}
}
And declaration of your column should be something similar to this:
GridTemplateColumn templateColumn =
new
GridTemplateColumn();
templateColumn.ItemTemplate =
new
MyTemplate(templateColumnName);
templateColumn.EditItemTemplate =
new
MyEditTemplate(templateColumn);
templateColumn.HeaderText = templateColumnName;
The other part for the ItemTemplate should be done in a similar way.
Addtinoally, more information about templates in asp.net you could find in this MSDN thread.
Greetings,
Andrey
the Telerik team

GridTemplateColumn templateColumn =
new
GridTemplateColumn();
templateColumn.ItemTemplate =
new
MyTemplate(templateColumnName);
templateColumn.EditItemTemplate =
new
MyEditTemplate(templateColumn);
templateColumn.HeaderText = templateColumnName;
I put in the code and still get the same problem of when I click on the edit button this column does not appear. I drilled down into the column variable and see the editItemTemplate but the IsEditable variable is false.
This seems to me as a non instantiated EditItemTemplate. Could you verify that you are specifying the EditItemTemplate right after the ItemTemplate and before adding the column to the Columns collection of the RadGrid.MasterTableView? Additionally, you could post the code where you are creating the Template column and instantiating the controls in its templates.
I am looking forward your reply.
Best wishes,
Andrey
the Telerik team

I've gone back and fort with the help topic and discussion in this thread but i still don get the hang of it..
My goal is to create the Radgrid from code behind due to my column size can vary from time to time depending how much row there is in certain table in my database.
i got the radgrid creation figured out, and i do can display my data
the problem is when it come to editing the data which i usually use "Template" as the EditFormSettings.
this is where i need help to enlight me regarding the how to.(please mind that i'm trying to create the whole formtemplate from code behind)
i managed to create the the itemtemplate class (maybe) but i don't know how to use it / is it correct or what
here is the template class:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
Telerik.Web.UI;
namespace
AuraTech.Giti.ReportSys.Site.Utils
{
public
class
CustomTemplate : ITemplate
{
public
int
NumOfColumns {
get
;
set
; }
public
string
ShipmentMonthPrefix {
get
;
set
; }
public
string
PricePrefix {
get
;
set
; }
public
List<
string
> fxNameList {
get
;
set
; }
public
CustomTemplate()
{
}
public
CustomTemplate(
int
totalPriceCol,
string
prefixShipmentMonth,
string
prefixPrice, List<
string
> FXnameList)
{
NumOfColumns = totalPriceCol;
ShipmentMonthPrefix = prefixShipmentMonth;
PricePrefix = prefixPrice;
fxNameList = FXnameList;
}
public
void
InstantiateIn(Control container)
{
CreateFutureExchangeEditTemplate(container);
}
private
void
CreateFutureExchangeEditTemplate(Control container)
{
Table table =
new
Table();
for
(
int
i = 0; i < NumOfColumns; i++)
{
Label lbl =
new
Label();
lbl.ID =
"lbl"
+ fxNameList[i] +
"Price"
;
lbl.Text = fxNameList[i].ToString();
RadNumericTextBox txtBox =
new
RadNumericTextBox();
txtBox.ID =
"txt"
+ fxNameList[i] +
"Price"
;
txtBox.Type = NumericType.Number;
txtBox.Text =
"<%#Bind("
+ PricePrefix + fxNameList[i] +
")%>"
;
txtBox.NumberFormat.DecimalDigits = 2;
RequiredFieldValidator validatorTextBox =
new
RequiredFieldValidator();
validatorTextBox.ID = fxNameList[i] +
"Required"
;
validatorTextBox.ControlToValidate = txtBox.ID;
validatorTextBox.ErrorMessage =
"*"
;
TableRow row =
new
TableRow();
TableCell cellLabel =
new
TableCell();
TableCell cellInput =
new
TableCell();
cellLabel.Controls.Add(lbl);
cellInput.Controls.Add(txtBox);
cellInput.Controls.Add(validatorTextBox);
row.Cells.Add(cellLabel);
row.Cells.Add(cellInput);
table.Rows.Add(row);
container.Controls.Add(table);
}
}
}
}
here is how i'm currently trying to add the template to the radgrid:
GridTemplateColumn templateCol =
new
GridTemplateColumn();
templateCol.EditItemTemplate =
new
CustomTemplate(totalPriceCol, prefix_shipmentMonth, price_prefix, fxNameList);
radGrid.MasterTableView.Columns.Add(templateCol);
When you have defined FormTemplate the templated controls inside the EditItemTemplate are not instantiated and get overridden by the FormTemplate controls.
You could check this online help topic for more information on how to create FormTemplates programmatically.
All the best,
Andrey
the Telerik team

thanks for your reply,
i've got the hang of it..
- for item template / edit form template (the whole form) you need to create a class that is partial class of that the template form (i forgot the exact name of class)
- use on need data source
- DEFINE THE GRID STRUCTURE ON PAGE_INIT EVENT!!
hahaha...
thanks,
Are these the steps to replicate the issue? No matter what is the type of the class from which you create the FormTemplate it will override the EditItemTemplate of the GridTemplateColumn.
I would suggest to create a sample project that replicates the issue and attach it to a formal support ticket. Thus I will be able to replicate the issue locally and let you know what is causing the issue.
All the best,
Andrey
the Telerik team

no i just don't understand the steps at first..
I've nailed now though..
thanks guys..,
These are requirements to work correctly with RadGrid and Templates. The first and the third conditions are inherited by the framework and are valid for all Template controls including the built-in ASP controls. The second condition is always the suggested approach, because it will save a lot of code and will provide better performance and last but not least the advanced features are supported only with NeedDataSource or declarative datasource.
Regards,
Andrey
the Telerik team