I'm trying to create a dropdown within a GridTemplateColumn. I don't
want to use the GridDropDownColumn because I want the dropdown to be
available always, not just during the edit call.
Simply put, I can't seem to target the dropdown to set it's selected index / selected value to the one that I desire.
Code snippets to follow.
snippet
Page file.
snippet
.cs file
The current form returns a standard, "System.NullReferenceException: Object reference not set to an instance of an object." exception, pointing at the line:
(DropDownList)dataItem["Quantity"].Controls[0]).SelectedValue = kvp.Value.ToString();
Am I accessing the cell incorrectly? I've tried setting the same code under the OnItemCreated method as well, but with the same results.
Thank you for any help you can provide.
-Josh
Simply put, I can't seem to target the dropdown to set it's selected index / selected value to the one that I desire.
Code snippets to follow.
snippet
Page file.
| <rad:RadGrid ID="rgOrder" runat="server" AllowPaging="False" AllowSorting="True" width="400" AllowAutomaticUpdates="true" OnItemCreated="rgItemCreated" OnItemDataBound="rgItemDataBound" OnNeedDataSource="rgNeedDataSource" AllowMultiEdit="True" |
| ShowFooter="false" CssClass="radGrid" EnableEmbeddedSkins="false" ShowHeader="true" AutoGenerateColumns="false" AllowMultiRowEdit="true"> |
| <MasterTableView EditMode="InPlace" DataKeyNames="ID"> |
| <ItemStyle CssClass="rgRow" /> |
| <FooterStyle CssClass="rgFooter"></FooterStyle> |
| <Columns> |
| <rad:GridBoundColumn DataField="ID" Display="false" /> |
| <rad:GridTemplateColumn HeaderText="Image" HeaderStyle-HorizontalAlign="left" HeaderStyle-Width="30" ItemStyle-Width="30" AllowFiltering="false"> |
| <ItemTemplate> |
| <img src="/images/demo_image_for_oc.jpg" alt="demo image"> |
| </ItemTemplate> |
| </rad:GridTemplateColumn> |
| <rad:GridBoundColumn readonly="true" DataField="Description" HeaderText="Description" HeaderStyle-Width="50" ItemStyle-Width="50" SortExpression="Description" HeaderStyle-HorizontalAlign="Left" AllowSorting="true" /> |
| <rad:GridTemplateColumn UniqueName="Quantity" HeaderText="Quantity" HeaderStyle-HorizontalAlign="left" HeaderStyle-Width="60" ItemStyle-Width="60" AllowFiltering="false"> |
| <ItemTemplate> |
| <asp:DropDownList runat="server" ID="ddlQuantity" DataTextField="Quantity" DataValueField="Quantity" DataSourceID="dataQuantity"> |
| </asp:DropDownList> |
| <asp:ObjectDataSource TypeName="Oser.Order" ID="dataQuantity" runat="server" SelectMethod="GetAllQuantities"> |
| </asp:ObjectDataSource> |
| </ItemTemplate> |
| </rad:GridTemplateColumn> |
| <rad:GridBoundColumn readonly="true" DataField="Price" HeaderText="Price" HeaderStyle-HorizontalAlign="left" HeaderStyle-Width="35" ItemStyle-Width="35" SortExpression="Price" AllowSorting="true" /> |
| <rad:GridBoundColumn readonly="true" DataField="QuantityPerUnit" HeaderStyle-HorizontalAlign="left" HeaderText="PerUnit" HeaderStyle-Width="40" ItemStyle-Width="40" SortExpression="PerUnit" AllowSorting="true" /> |
| <rad:GridEditCommandColumn ButtonType="ImageButton" ItemStyle-CssClass="btnEdit" EditImageUrl="~/images/btn_edit.jpg" HeaderStyle-Width="35" ItemStyle-Width="35" /> |
| <rad:GridTemplateColumn HeaderText="" HeaderStyle-Width="35" ItemStyle-Width="35" AllowFiltering="false"> |
| <ItemTemplate> |
| <asp:ImageButton ID="btnDelete" CssClass="btnDelete" OnClientClick="return confirm('Are you sure you want to delete this item?');" OnCommand="DeleteItem" CommandArgument='<%# Eval("ID") %>' ImageUrl="/images/btn_delete.jpg" runat="server" /> |
| </ItemTemplate> |
| </rad:GridTemplateColumn> |
| </Columns> |
| </MasterTableView> |
| </rad:RadGrid> |
snippet
.cs file
| protected void rgItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) |
| { |
| Dictionary<int, int> orderItems = (Dictionary<int, int>)Session["orderItems"]; |
| if (orderItems != null && orderItems.Count > 0) |
| { |
| foreach (KeyValuePair<int, int> kvp in orderItems) |
| { |
| GridDataItem dataItem = e.Item as GridDataItem; |
| (DropDownList)dataItem["Quantity"].Controls[0]).SelectedValue = kvp.Value.ToString(); |
| } |
| } |
| } |
The current form returns a standard, "System.NullReferenceException: Object reference not set to an instance of an object." exception, pointing at the line:
(DropDownList)dataItem["Quantity"].Controls[0]).SelectedValue = kvp.Value.ToString();
Am I accessing the cell incorrectly? I've tried setting the same code under the OnItemCreated method as well, but with the same results.
Thank you for any help you can provide.
-Josh