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

Dynamically Creating a GridTemplateColumn

8 Answers 539 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jordan Parfitt
Top achievements
Rank 1
Jordan Parfitt asked on 25 Apr 2008, 05:06 PM
I have a radgrid in my ASPX page. Depending on what the user asks for add add columns to this grid dynamically.

One of the columns is a GridTemplateColumn. I add an image to this column.

It all loads properly on the first grid bind, however if I click the view next page feature on the rad grid this column disappears.

Has anyone had experience with this?

Thanks,
Jordan Parfitt

8 Answers, 1 is accepted

Sort by
0
Jordan Parfitt
Top achievements
Rank 1
answered on 25 Apr 2008, 05:57 PM
After further review my column seems to dissappear on all postbacks - anyone know why?
0
Vladimir
Top achievements
Rank 1
answered on 26 Apr 2008, 07:05 AM
Hi Jordan,

When the grid declaration is on the page you should create columns similar to this example:
http://www.telerik.com/demos/aspnet/prometheus/Grid/Examples/Programming/GroupBy/DefaultCS.aspx

Creating template column in this scenario is not supported - you can create the entire grid dynamically similar to this example:
http://www.telerik.com/demos/aspnet/prometheus/Grid/Examples/Hierarchy/DefiningStructure/DefaultCS.aspx

0
Jordan Parfitt
Top achievements
Rank 1
answered on 28 Apr 2008, 01:24 PM
So am I to understand that it is not possible to dynamically create a GridTemplateColumn?


Thanks,

Jordan
0
Konstantin Petkov
Telerik team
answered on 29 Apr 2008, 07:10 AM
Hello Jordan,

You can create GridTemplateColumns dynamically as well. I believe you will find the following article helpful (look at the "Creating template columns programmatically" part):

http://www.telerik.com/help/radcontrols/aspnet-ajax/?grdProgrammaticCreation.html

Sincerely yours,
Konstantin Petkov
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jordan Parfitt
Top achievements
Rank 1
answered on 29 Apr 2008, 01:13 PM
In the example you just showed me... as well as in mine, all the images/textboxes/chechbox/etc. are no longer visible on post-back. Why is this and is there a way to prevent it?

Thanks,
Jordan

        private void CreateTemplateColumn(RadGrid Grid, string datafield, string headertext, int index)
        {
            GridTemplateColumn tmpColumn = new GridTemplateColumn();
            Grid.MasterTableView.Columns.AddAt(index, tmpColumn); //Add column before setting properties
            tmpColumn.HeaderText = headertext;
            tmpColumn.UniqueName = datafield;
            tmpColumn.DataField = datafield;
            tmpColumn.ItemTemplate = new Flags();
        }

    public class Flags : ITemplate
    {
        protected Image imgFlag;

        public void InstantiateIn(System.Web.UI.Control container)
        {
            imgFlag = new Image();
            imgFlag.ID = "imgFlag";
            imgFlag.ImageUrl = "~/images/document.gif";
            imgFlag.DataBinding += new EventHandler(imgFlag_DataBinding);
            container.Controls.Add(imgFlag);
        }
    }

0
Jordan Parfitt
Top achievements
Rank 1
answered on 29 Apr 2008, 01:17 PM
If it makes a difference I am adding my Grid on the ASPX, then adding columns dynamically.


                    <td valign="top">
                        <asp:Label ID="lblquad1" runat="server" Font-Bold="true"></asp:Label>
                        <radG:RadGrid ID="rgQuad1" runat="server" GridLines="None" AutoGenerateColumns="false" Skin="Office2007" AllowSorting="True" AllowPaging="true" OnDataBound="rgQuad1_OnDataBound" OnNeedDataSource="rgQuad1_OnNeedDataSource" OnItemCommand="rgQuad1_ItemCommand" EnableViewState="true">
                               <MasterTableView PageSize="25" CommandItemDisplay="Bottom">     
                                    <CommandItemTemplate>
                                        <asp:ImageButton ID="btnExcel" ImageUrl="../images/excel.png" ToolTip="Export to MS Excel" Visible='<%# !rgQuad1.MasterTableView.IsItemInserted %>' runat="server" CommandName="Excel" OnDataBinding="btnExcel1_OnDataBinding" OnClientClick="javascript:ExportToExcel(1)"/>
                                    </CommandItemTemplate>
                                    <ExpandCollapseColumn Visible="False"><HeaderStyle Width="19px" /></ExpandCollapseColumn>
                                    <RowIndicatorColumn Visible="False"><HeaderStyle Width="20px" /></RowIndicatorColumn>
                                </MasterTableView>
                        </radG:RadGrid>
                    </td>
0
Kiara
Top achievements
Rank 1
answered on 05 May 2008, 09:09 AM
I read the documentation topic linked in the previous post and it states that mixes load of columns is not supported by the Telerik grid. Moreover, it indicates that when generating template columns you have to build your grid entirely programmatically on PageInit and add the columns to the Columns collection after you set their properties.

Having in mind these details, correct your code to see how it goes.

Kiara
0
Jordan Parfitt
Top achievements
Rank 1
answered on 05 May 2008, 12:37 PM
When I built it all programmatically instead of having the radgrids on the ASPX it worked fine. Thanks
Tags
Grid
Asked by
Jordan Parfitt
Top achievements
Rank 1
Answers by
Jordan Parfitt
Top achievements
Rank 1
Vladimir
Top achievements
Rank 1
Konstantin Petkov
Telerik team
Kiara
Top achievements
Rank 1
Share this question
or