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

creating GridTemplateColumn in code behind

14 Answers 1019 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Fred
Top achievements
Rank 1
Fred asked on 16 Nov 2011, 08:38 PM

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

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Nov 2011, 04:52 AM
Hello Fred,

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

-Shinu.
0
Fred
Top achievements
Rank 1
answered on 17 Nov 2011, 03:14 PM
sorry but I have gone over that a couple times and am not getting it. if you could just show me by translating this code then I will understand what is going on.
0
Shinu
Top achievements
Rank 2
answered on 18 Nov 2011, 05:36 AM
Hello Fred,

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.
0
Fred
Top achievements
Rank 1
answered on 18 Nov 2011, 03:17 PM
this is sort of the same example I've already seen. as you can see I want to see the value in the grid but when in edit mode I want a drop down list. Again if you could just translate my original message:

<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 
0
Fred
Top achievements
Rank 1
answered on 23 Nov 2011, 02:08 PM
have you noticed your examples don't use an edit for?
0
Andrey
Telerik team
answered on 23 Nov 2011, 04:19 PM
Hello Fred,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Fred
Top achievements
Rank 1
answered on 23 Nov 2011, 04:53 PM
I assume you meant templateColumnName and not templateColumn in the new MyEditTemplate. (See Below)
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.
0
Andrey
Telerik team
answered on 24 Nov 2011, 05:17 AM
Hi Fred,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Nahwin
Top achievements
Rank 1
answered on 21 Feb 2013, 03:23 AM
Hi there,

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);
0
Andrey
Telerik team
answered on 25 Feb 2013, 03:30 PM
Hello,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Nahwin
Top achievements
Rank 1
answered on 03 Mar 2013, 03:20 PM
Hi there Andrey,

thanks for your reply,
i've got the hang of it.. 

  1. 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)
  2. use on need data source
  3. DEFINE THE GRID STRUCTURE ON PAGE_INIT EVENT!!

hahaha...
thanks,
0
Andrey
Telerik team
answered on 06 Mar 2013, 03:28 PM
Hi,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Nahwin
Top achievements
Rank 1
answered on 09 Mar 2013, 07:05 AM
hi,

no i just don't understand the steps at first..
I've nailed now though..


thanks guys..,
0
Andrey
Telerik team
answered on 13 Mar 2013, 01:45 PM
Hello,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Fred
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Fred
Top achievements
Rank 1
Andrey
Telerik team
Nahwin
Top achievements
Rank 1
Share this question
or