Dynamically Creating a GridTemplateColumn

9 posts, 0 answers
  1. Jordan Parfitt
    Jordan Parfitt avatar
    17 posts
    Member since:
    Aug 2007

    Posted 25 Apr 2008 Link to this post

    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
  2. Jordan Parfitt
    Jordan Parfitt avatar
    17 posts
    Member since:
    Aug 2007

    Posted 25 Apr 2008 Link to this post

    After further review my column seems to dissappear on all postbacks - anyone know why?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Vlad
    Vlad avatar
    115 posts
    Member since:
    Dec 2007

    Posted 26 Apr 2008 Link to this post

    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

  5. Jordan Parfitt
    Jordan Parfitt avatar
    17 posts
    Member since:
    Aug 2007

    Posted 28 Apr 2008 Link to this post

    So am I to understand that it is not possible to dynamically create a GridTemplateColumn?


    Thanks,

    Jordan
  6. Konstantin Petkov
    Admin
    Konstantin Petkov avatar
    1911 posts

    Posted 29 Apr 2008 Link to this post

    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
  7. Jordan Parfitt
    Jordan Parfitt avatar
    17 posts
    Member since:
    Aug 2007

    Posted 29 Apr 2008 Link to this post

    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);
            }
        }

  8. Jordan Parfitt
    Jordan Parfitt avatar
    17 posts
    Member since:
    Aug 2007

    Posted 29 Apr 2008 Link to this post

    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>
  9. Kiara
    Kiara avatar
    309 posts
    Member since:
    Jul 2006

    Posted 05 May 2008 Link to this post

    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
  10. Jordan Parfitt
    Jordan Parfitt avatar
    17 posts
    Member since:
    Aug 2007

    Posted 05 May 2008 Link to this post

    When I built it all programmatically instead of having the radgrids on the ASPX it worked fine. Thanks
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017