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

RadGrid GridTemplateColumn used in page init, now getting multiple columns

2 Answers 185 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Weeble
Top achievements
Rank 1
Weeble asked on 06 Sep 2011, 05:58 PM
Hi,

In my project, I have a number of known columns, which are in the .aspx page:

<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"
            AllowAutomaticInserts="True" PageSize="5" AllowAutomaticUpdates="True" AllowPaging="True"
            AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_OnNeedRoleDataSource" OnItemUpdated="RadGrid1_ItemUpdated"
            OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted" OnDataBound="RadGrid1_DataBound" OnItemDataBound="RadGrid1_OnRoleItemDataBound">
            <PagerStyle Mode="NextPrevAndNumeric" />
            <MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" DataKeyNames="RoleID" EditMode="InPlace" HorizontalAlign="NotSet" AutoGenerateColumns="False">
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <ItemStyle CssClass="MyImageButton" />
                    </telerik:GridEditCommandColumn>
                                         
                    <telerik:GridTemplateColumn HeaderText="RoleTitle" SortExpression="Role" UniqueName="Role" EditFormColumnIndex="1">
                        <ItemTemplate>
                            <asp:Label ID="lblRoleTitle" runat="server" Text='<%# Eval("RoleTitle") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>                        
                            <asp:DropDownList ID="cbRoleTitle" runat="server" />                           
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                     
                    <telerik:GridTemplateColumn HeaderText="Type" SortExpression="EmpType" UniqueName="EmpType" EditFormColumnIndex="2">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblEmployeeType" Text='Staff'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>                           
                            <asp:DropDownList ID="cbEmploymentType" runat="server" />                                                           
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                                         
                    <telerik:GridTemplateColumn HeaderText="Notes" SortExpression="Notes" UniqueName="Notes" EditFormColumnIndex="3">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblNotes" Text='Notes'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <span>
                                <asp:TextBox ID="tbNotes" runat="server" Rows="5" Wrap="true" />                               
                            </span>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                     
                    <telerik:GridTemplateColumn HeaderText="Discipline" SortExpression="Discipline" UniqueName="Discipline" EditFormColumnIndex="4">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblDiscipline" Text='Senior'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <span>
                                <asp:DropDownList ID="cbDiscipline" runat="server"/>                               
                            </span>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>                 
 
                </Columns>               
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnRowDblClick="RowDblClick" />
            </ClientSettings>
        </telerik:RadGrid>

 

 

And also a set of dynamic columns, that the GridTemplate is used for, and is created in the page_init.

The problem is that I now get multiples of these dynamic columns whenever the page refreshes.

If I check for Page.IsPostBack, then the edit columns just don't work when I add a new row.

Any idea how I can stop seeing duplicate columns?

Thanks

protected void Page_Init(object sender, EventArgs e)
        {
            //And the weekly columns
            if (RadGrid1.Columns.Count < 6)
            {
                SPWeb web = SPContext.Current.Web;
 
                //Need a value cell per week - this code builds up a set of columns from the FR.ValueColumn
                if (FR!= null)
                {
                    List<ValueColumn> valueCols = GetValueColumnsByID(web, FR.ID);
 
                    if (valueCols != null && valueCols.Count > 0)
                    {
                        foreach (ValueColumn value in valueCols)
                        {
                            //The grid needs to be added BEFORE the values are assigned, otherwise headings are lost when in Edit mode
                           GridTemplateColumn tc1 = new GridTemplateColumn();
                            RadGrid1.MasterTableView.Columns.Add(tc1);
 
                            tc1.ItemTemplate = new DataGridTemplate(ListItemType.Item, "VC:" + value.ID.ToString());
                            tc1.EditItemTemplate = new DataGridTemplate(ListItemType.EditItem, "VC:" + value.ID.ToString());
                            tc1.HeaderText = value.Title;
                            tc1.UniqueName = "ValueColumn-" + value.ID.ToString();
                            tc1.DataField = "x";
 
                        }
                    }
                }
            }
        }

 

 

 

 

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 07 Sep 2011, 04:44 AM
Hello Weeble,

RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file.

Please take a look into the following documentation for more on this.
Programmatic Creation.

Thanks,
Shinu.
0
Weeble
Top achievements
Rank 1
answered on 08 Sep 2011, 10:33 AM
Thanks, its a bit annoying, but seemed to do the trick.  The grid is now created programatically in the page_init.



Tags
Grid
Asked by
Weeble
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Weeble
Top achievements
Rank 1
Share this question
or