I create the grid below:
<telerik:RadGrid runat="server" ID="grdImportData" RenderMode="Lightweight" AllowPaging="true" PageSize="10" OnNeedDataSource="grdImportData_NeedDataSource" OnPageIndexChanged="grdImportData_PageIndexChanged" OnUpdateCommand="grdImportData_UpdateCommand" OnDeleteCommand="grdImportData_DeleteCommand" OnItemCommand="grdImportData_ItemCommand" OnItemDataBound="grdImportData_ItemDataBound" AllowSorting="False" AlternatingItemStyle-BackColor="#f5f5f5" ItemStyle-BackColor="#ffffff" ColumnWidth="Auto" HorizontalAlignment="Stretch" Width="100%"> <ClientSettings> <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="true" FrozenColumnsCount="1" EnableVirtualScrollPaging="true"></Scrolling> <Resizing AllowColumnResize="true" ResizeGridOnColumnResize="true" EnableRealTimeResize="true" /> </ClientSettings> <MasterTableView EditMode="InPlace" DataKeyNames="Id"> <Columns> <telerik:GridEditCommandColumn HeaderStyle-Width="75px" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/> <telerik:GridTemplateColumn HeaderStyle-Width="50px"> <ItemTemplate> <asp:LinkButton runat="server" CommandName="Delete"><i class="icon s7-junk grid-edit-icon"></i></asp:LinkButton> </ItemTemplate> </telerik:GridTemplateColumn> </Columns> </MasterTableView></telerik:RadGrid>
I then try to add a TemplateColumn, EditTemplateColumn and RadDropdownlist programmatically like below:
protected void grdImportData_ItemDataBound(object sender, GridItemEventArgs e){ if(ImportType == "CLEARGISTIX_ASSETS") { if (e.Item is GridDataItem) { string templateColumnName = "AssetTypeName"; GridTemplateColumn templateColumn = new GridTemplateColumn(); templateColumn.ItemTemplate = new MyTemplate(templateColumnName); templateColumn.EditItemTemplate = new MyEditTemplate(); templateColumn.HeaderText = templateColumnName; templateColumn.DataField = "AssetTypeName"; if (e.Item.IsInEditMode) { RadDropDownList rddl = new RadDropDownList(); PortalView.LookupListBO list = LookupListBA.LookupList_GetByKey(DB_Context, "SITE_ASSETTYPE_LIST", UtilityBA.IsActiveChoice.Active); List<PortalView.LookupListItemBO> oList = LookupListBA.LookupListItem_GetList_ByLookupListId(DB_Context, list.LookupListId, (Guid)Current.Employee.SiteId); rddl.ID = "dropdownlist1"; rddl.DataSource = oList; rddl.DataTextField = "Name"; rddl.DataValueField = "LookupListItemId"; } grdImportData.MasterTableView.Columns.Add(templateColumn); } }}public class MyTemplate : ITemplate{ private string colname; protected Label lControl; public MyTemplate(string cName) { colname = cName; } public void InstantiateIn(System.Web.UI.Control container) { lControl = new Label(); lControl.ID = "lControl"; lControl.DataBinding += new EventHandler(lControl_DataBinding); container.Controls.Add(lControl); } public void lControl_DataBinding(object sender, EventArgs e) { Label l = (Label)sender; GridDataItem container = (GridDataItem)l.NamingContainer; l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />"; }}public class MyEditTemplate : IBindableTemplate{ public void InstantiateIn(Control container) { GridEditFormItem item = ((GridEditFormItem)(container.NamingContainer)); RadDropDownList drop = new RadDropDownList(); drop.ID = "dropdownlist1"; container.Controls.Add(drop); } public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container) { OrderedDictionary od = new OrderedDictionary(); od.Add("LookupListItemId", ((RadDropDownList)(((GridEditFormItem)(container)).FindControl("dropdownlist1"))).DataValueField); return od; }}
When I click on the edit button, I error: 'Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridEditFormItem'.' on this line:
od.Add("LookupListItemId", ((RadDropDownList)(((GridEditFormItem)(container)).FindControl("dropdownlist1"))).DataValueField);I notice that it never his the InstaniateIn code at all.
Any assistance is greatly appreciated.
