How To Populate DropDown in Grid

14 posts, 1 answers
  1. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 30 Mar 2010 Link to this post

    I've been playing with this for a week or so now with no luck.

    I have a grid....

    <telerik:RadGrid ID="Grid1" AutoGenerateColumns="false" runat="server" Skin="Web20" Width="90%"
                <MasterTableView> 
                    <Columns> 
                        <telerik:GridBoundColumn HeaderText="" UniqueName="WDFields" DataField="friendlyname" /> 
                        <telerik:GridTemplateColumn UniqueName="ddHeader" HeaderText="Header"
                            <EditItemTemplate> 
                                <asp:DropDownList ID="HeaderDropDown" runat="server" />                         
                            </EditItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 

    And need to populate the DropDownList (HeaderDropDown) from a seperate datasource than what populates the Grid in general.

    Currently, in the code behind, I have...

    private void Grid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item.IsInEditMode) 
            { 
                GridEditableItem item = e.Item as GridEditableItem; 
                Dictionary<string, string> dict = new Dictionary<string, string>(); 
                dict = ReadHeader(); 
                DropDownList list = item.FindControl("HeaderDropDown") as DropDownList; 
                list.DataSource = dict
                list.DataBind(); 
            } 
        } 

    I have a breakpoint set at the if statement and I'm never stepping into it.

    Thanks.


  2. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 30 Mar 2010 Link to this post

    I believe this help article should be of use to you.



    Also, if the RadGrid syntax you posted is what your actually using then I noticed that you didn't attach the handler to the grid control. If you do have it attached then I don't understand why it doesn't work since your code matches the help article, although it does add a check to ensure the item being bound is a GridEditableItem.

    I hope that helps.

  3. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 30 Mar 2010 Link to this post

    This is the actual code.  Can you elaborate on, "attach the handler to the grid control"?

  4. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 30 Mar 2010 Link to this post

    You need to attach the handler to the RadGrid control in order for the event to be raised.


    Below is the modified version of the RadGrid declaration with the event handler attached:
    <telerik:RadGrid ID="Grid1" AutoGenerateColumns="false" runat="server" Skin="Web20" Width="90%" OnItemDataBound="Grid1_ItemDataBound">  
                <MasterTableView>  
                    <Columns>  
                        <telerik:GridBoundColumn HeaderText="" UniqueName="WDFields" DataField="friendlyname" />  
                        <telerik:GridTemplateColumn UniqueName="ddHeader" HeaderText="Header">  
                            <EditItemTemplate>  
                                <asp:DropDownList ID="HeaderDropDown" runat="server" />                          
                            </EditItemTemplate>  
                        </telerik:GridTemplateColumn>  
                    </Columns>  
                </MasterTableView>  
            </telerik:RadGrid>  

    You'll notice that I've added OnItemDataBound="Grid1_ItemDataBound" to the RadGrid declaration. That's how you attach events.

  5. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 31 Mar 2010 Link to this post

    does this do the same thing?

     private void Page_Load(object sender, EventArgs e) 
        {  
            Grid1.NeedDataSource += new GridNeedDataSourceEventHandler(Grid1_NeedDataSource); 
            Grid1.ItemDataBound += new GridItemEventHandler(Grid1_ItemDataBound); 

            param = ReadParam(); 
        } 

  6. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 31 Mar 2010 Link to this post

    I changed the routine to the following...

    private void Grid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridDataItem) 
            { 
                GridDataItem item = (GridDataItem) e.Item; 
                Dictionary<string, string> dict = new Dictionary<string, string>(); 
                dict = ReadHeader(); 
                DropDownList list = (DropDownList) item.FindControl("HeaderDropDown"); 
                list.DataSource = dict
                list.DataBind(); 
            } 
        } 

    and am FINALLY stepping into the if statement.  However, I cannot find "HeaderDropDown".  Any suggestions, PLEASE?

  7. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 31 Mar 2010 Link to this post

    The reason your not finding the control is because your looking for a GridDataItem, instead of a GridEditableItem. So if you change GridDataItem to GridEditableItem in your code, you should be able to find the control.


    I hope that helps.

  8. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 31 Mar 2010 Link to this post

    Nope, now I have...

    private void Grid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item is GridEditableItem) 
            { 
                GridEditableItem item = (GridEditableItem) e.Item; 
                Dictionary<string, string> dict = new Dictionary<string, string>(); 
                dict = ReadHeader(); 
                DropDownList list = (DropDownList) item.FindControl("HeaderDropDown"); 
                list.DataSource = dict
                list.DataBind(); 
            } 
        } 

    and STILL not finding the drop down.

    Thanks.

  9. Pavlina
    Admin
    Pavlina avatar
    5246 posts
    Member since:
    Sep 2012

    Posted 01 Apr 2010 Link to this post

    Hello Tim,

    Please take a look at the following resources demonstrating how the desired by you functionality can be achieved:
    Column Types
    Customize/Configure GridDropDownColumn
    Different edit forms on edit and insert

    I hope this gets you started properly.

    All the best,
    Pavlina
    the Telerik team


    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.

  10. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 01 Apr 2010 Link to this post

    I have gone through all of this already.  And as noted by someone else above, my code matches the examples in these docs.  Are you able to get this to work?

  11. robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 01 Apr 2010 Link to this post

    Well the only difference between your code and the one shown in the help is that it checks if the item is in edit mode.



    private void Grid1_ItemDataBound(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
            {  
                GridEditableItem item = (GridEditableItem) e.Item;  
                Dictionary<stringstring> dict = new Dictionary<stringstring>();  
                dict = ReadHeader();  
                DropDownList list = (DropDownList) item.FindControl("HeaderDropDown");  
                list.DataSource = dict;  
                list.DataBind();  
            }  
        } 

    Maybe that will make a difference.

  12. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 01 Apr 2010 Link to this post

    I started out with that. When I add " && e.Item.IsInEditMode" I never step into the if statement.  So, it's not in edit mode but the dropdown is in a <EditItemTemplate> block.  I just don't get it.

    I've tried working numerous AJAX samples and have never had luck getting any of them to work, as written.

  13. Answer
    robertw102
    robertw102 avatar
    265 posts
    Member since:
    Jul 2007

    Posted 01 Apr 2010 Link to this post

    I think you should place the DropDownList inside of ItemTemplate, instead of EditItemTemplate, since it looks like your not using the edit functionality of the RadGrid (you don't have a button that initiates the edit mode of the grid). Then change your code back to using GridDataItem, instead of GridEditableItem.


    I hope that helps.

  14. tim
    tim avatar
    21 posts
    Member since:
    Sep 2008

    Posted 01 Apr 2010 Link to this post

    This did it, thanks!!

Back to Top