For the last few weeks I have been trying to populate the datasource of a raddropdownlist inside a radgridcolumn -but without success. I have looked at various examples online and in the help files -but none of them seem to work for me.
I thought that maybe one of you knew why I can't get my code to populate the dropdownlist.
The problem seems to be that (e.Item is GridEditableItem && e.Item.IsEditMode) is never true in my grid and therefore never evaluates the code inside the if statement that would bind the dropdownlist... Thanks in advance. YMA
Here is an example of the grid and the dropdownlist (CategoryIDDropDown) that I can't populate :
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{ if (e.Item is GridEditableItem && e.Item.IsInEditMode) { GridEditableItem item = e.Item as GridEditableItem; // access/modify the edit item template settings here DropDownList list = item.FindControl("CategoryIDDropDown") as DropDownList; list.DataSource = mydatasource; list.DataBind(); { {
<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"
AllowAutomaticInserts="True" PageSize="10" Skin="Default" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted"
OnItemUpdated="RadGrid1_ItemUpdated" AllowAutomaticUpdates="True" AllowPaging="True"
AutoGenerateColumns="False" OnBatchEditCommand="RadGrid1_BatchEditCommand" DataSourceID="SqlDataSource1" Height="500px" OnItemCommand="RadGrid1_ItemCommand" Visible="True">
<ExportSettings>
<Pdf>
<PageHeader>
<LeftCell Text="" />
<MiddleCell Text="" />
<RightCell Text="" />
</PageHeader>
<PageFooter>
<LeftCell Text="" />
<MiddleCell Text="" />
<RightCell Text="" />
</PageFooter>
</Pdf>
</ExportSettings>
<MasterTableView AutoGenerateColumns="False" BatchEditingSettings-EditType="Row" CommandItemDisplay="Bottom" DataKeyNames="ProductID" DataSourceID="SqlDataSource1" EditMode="Batch" HorizontalAlign="NotSet">
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<BatchEditingSettings EditType="Cell" />
<SortExpressions>
<telerik:GridSortExpression FieldName="ProductID" SortOrder="Descending" />
</SortExpressions>
<CommandItemSettings ExportToPdfText="Export to PDF" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ProductName" HeaderStyle-Width="210px" HeaderText="ProductName" SortExpression="ProductName" UniqueName="ProductName">
<ColumnValidationSettings EnableRequiredFieldValidation="true">
<RequiredFieldValidator ForeColor="Red" Text="*This field is required">
</RequiredFieldValidator>
<ModelErrorMessage Text="" />
</ColumnValidationSettings>
<HeaderStyle Width="210px" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn DataField="CategoryID" HeaderStyle-Width="180px" HeaderText="Category" UniqueName="CategoryID">
<ItemTemplate>
<%# Eval("CategoryName") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList ID="CategoryIDDropDown" runat="server" DataTextField="CategoryName" DataValueField="CategoryID">
</telerik:RadDropDownList>
</EditItemTemplate>
<HeaderStyle Width="180px" />
</telerik:GridTemplateColumn>
<telerik:GridNumericColumn ColumnEditorID="NumericEditor1" DataField="UnitsInStock" HeaderStyle-Width="80px" HeaderText="Units In Stock" SortExpression="UnitsInStock" UniqueName="UnitsInStock">
<ColumnValidationSettings>
<ModelErrorMessage Text="" />
</ColumnValidationSettings>
<HeaderStyle Width="80px" />
</telerik:GridNumericColumn>
<telerik:GridBoundColumn ColumnEditorID="TextEditor" DataField="QuantityPerUnit" HeaderStyle-Width="135px" HeaderText="Quantity Per Unit" SortExpression="QuantityPerUnit" UniqueName="QuantityPerUnit">
<ColumnValidationSettings>
<ModelErrorMessage Text="" />
</ColumnValidationSettings>
<HeaderStyle Width="135px" />
</telerik:GridBoundColumn>
<telerik:GridCheckBoxColumn DataField="Discontinued" HeaderStyle-Width="80px" HeaderText="Discontinued" SortExpression="Discontinued" UniqueName="Discontinued">
<HeaderStyle Width="80px" />
</telerik:GridCheckBoxColumn>
<telerik:GridTemplateColumn ColumnEditorID="NumericEditor1" DataField="UnitPrice" HeaderStyle-Width="80px" HeaderText="UnitPrice" SortExpression="UnitPrice" UniqueName="TemplateColumn">
<ItemTemplate>
<asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<span>
<telerik:RadNumericTextBox ID="tbUnitPrice" runat="server" Width="55px">
</telerik:RadNumericTextBox>
<span style="color: Red">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbUnitPrice" ErrorMessage="*Required">
</asp:RequiredFieldValidator>
</span></span>
</EditItemTemplate>
<HeaderStyle Width="80px" />
</telerik:GridTemplateColumn>
<telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmDialogType="RadWindow" ConfirmText="Delete this product?" ConfirmTitle="Delete" HeaderStyle-Width="50px" HeaderText="Delete" Text="Delete" UniqueName="DeleteColumn">
<HeaderStyle Width="50px" />
</telerik:GridButtonColumn>
</Columns>
<PagerStyle PageSizeControlType="RadComboBox" />
</MasterTableView>
<HeaderStyle BackColor="Silver" ForeColor="White" />
<PagerStyle PageSizeControlType="RadComboBox" />
<FilterMenu EnableImageSprites="False">
</FilterMenu>
</telerik:RadGrid>
<telerik:GridNumericColumnEditor ID="NumericEditor1" runat="server">
<NumericTextBox ID="NumericTextBox1" runat="server" Width="60px" />
</telerik:GridNumericColumnEditor>
<telerik:GridTextBoxColumnEditor runat="server" ID="TextEditor">
<TextBoxStyle Width="120px" />
</telerik:GridTextBoxColumnEditor>
</telerik:RadAjaxPanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [CategoryID], [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock]) VALUES (@ProductName, @CategoryID, @UnitPrice, @Discontinued, @QuantityPerUnit, @UnitsInStock)"
SelectCommand="SELECT [ProductID], [ProductName], [Products].[CategoryID], [Categories].[CategoryName] as CategoryName, [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock] FROM [Products] JOIN Categories ON Products.CategoryID=Categories.CategoryID"
UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID, [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued, [QuantityPerUnit] = @QuantityPerUnit, [UnitsInStock] = @UnitsInStock WHERE [ProductID] = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
<asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
<asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
<asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
<asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
<asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
<asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
<asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
<asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
<asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>