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

Problem creating Radgrid GridTemplateColumn dynamically

3 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peeyush
Top achievements
Rank 1
Peeyush asked on 04 Dec 2012, 12:50 PM
Hello,

I am facing lot of problem creating GridTemplateColumn dynamically, i tried it using implementing ITemplate interface but was not successful.

I have other columns too in grid, which i create dynamically and do databinding also dynamically which works perfect,

Please can someone help me through this, its urgent.

Thanks in advance!

with regards,
Peeyush pandey
<telerik:GridTemplateColumn AllowFiltering="False" UniqueName="columnAction"  >
    <HeaderStyle Width="30px" />
    <HeaderTemplate>
        <asp:Label ID="lblHeaderAction" runat="server" Text="Action"></asp:Label>
    </HeaderTemplate>
    <ItemTemplate>
        <asp:Panel ID="editButtonPanel" runat="server" CssClass="custom-Action-column" Width="50px">
            <asp:ImageButton ID="TagCloudButton" runat="server"  Width="15px"
                Height="15px" CommandName="TagCloud"   CausesValidation="False" ImageUrl="~/images/icon_grid.gif" />
        </asp:Panel>
    </ItemTemplate>
</telerik:GridTemplateColumn>
 
 
 <telerik:GridTemplateColumn AllowFiltering="False" UniqueName="ProfileImage"  >
    <HeaderStyle Width="50px" />
    <HeaderTemplate>
        <asp:Label ID="lblHeaderActionImage" runat="server" Text="Image"></asp:Label>
    </HeaderTemplate>
    <ItemTemplate>
     <asp:Panel ID="ImagePanelPos" runat="server" CssClass="custom-Action-column" Width="50px">
        <asp:Image ID="Image1" runat="server" Height="35px" Width="35px" ImageUrl='<%#Convert.ToString(Eval("Url")) %>' />
        </asp:Panel>
    </ItemTemplate>
</telerik:GridTemplateColumn>
 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Dec 2012, 04:29 AM
Hi,

You can create template columns dynamically in the Page_Init event handler. 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 or EditTemplateTemplate property of the GridTemplateColumn object. Check the following help documentation which explains more about this.
Programmatic Creation

Thanks,
Shinu.
0
Peeyush
Top achievements
Rank 1
answered on 06 Dec 2012, 04:58 AM
Hello Shinu,

Thanks for your help but,I have already gone through this link, and did things the similar way but still could not succeed, could you please help me out through an illustartion through code of one column which consist of header template and an item template(consisting of asp panel) like the similar one i posted below.



It would be of great help, as i need to figure out where i am going wrong.

Thanks!


regards,
Peeyush Pandey
0
Accepted
Shinu
Top achievements
Rank 2
answered on 06 Dec 2012, 05:24 AM
Hi,

Here is the sample code to create a template column dynamically.
C#:
protected void Page_Init(object sender, System.EventArgs e)
{
  RadGrid1 = new RadGrid();
  RadGrid1.ID = "RadGrid1";
  RadGrid1.MasterTableView.DataKeyNames = new string[] { "EmployeeID" };
  RadGrid1.AllowPaging = true;
  RadGrid1.MasterTableView.AutoGenerateColumns = false;
  RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
  NewTemplateColumn templateColumn = new NewTemplateColumn();
  templateColumn.ItemTemplate = new TemplateColumn();
  RadGrid1.MasterTableView.Columns.Add(templateColumn);
  GridEditCommandColumn edit;
  edit = new GridEditCommandColumn();
  RadGrid1.MasterTableView.Columns.Add(edit);
  GridBoundColumn boundColumn;
  boundColumn = new GridBoundColumn();
  RadGrid1.MasterTableView.Columns.Add(boundColumn);
  boundColumn.DataField = "EmployeeID";
  boundColumn.HeaderText = "EmployeeID";
  boundColumn.UniqueName = "EmployeeID";
  PlaceHolder1.Controls.Add(RadGrid1);
}
public partial class NewTemplateColumn : GridTemplateColumn
    {
        public void InstantiateIn(Control container)
        {
         
        }
    }
public partial class TemplateColumn : ITemplate
{
 public void InstantiateIn(Control container)
{
   Button btn = new Button();
   btn.Text = "Sample";
   btn.ID = "Button1";
   container.Controls.Add(btn);
 }
}

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