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

[Solved] Dropdown targetting issues in GridTemplateColumn

1 Answer 140 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 04 Mar 2008, 08:38 PM
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.
<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

1 Answer, 1 is accepted

Sort by
0
Josh
Top achievements
Rank 1
answered on 04 Mar 2008, 09:48 PM
Figured out my own issue, bah.

I just needed to toss in a check for:

if (e.Item is GridDataItem)

around the dropdownlist clause. I think it was looking in the header as part of it's procedure based on the rest of the code, and obviously not finding a dropdownlist there.
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Josh
Top achievements
Rank 1
Share this question
or