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

How To Populate DropDown in Grid

13 Answers 421 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tim
Top achievements
Rank 1
tim asked on 30 Mar 2010, 06:46 PM
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.


13 Answers, 1 is accepted

Sort by
0
robertw102
Top achievements
Rank 1
answered on 30 Mar 2010, 08:20 PM
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.
0
tim
Top achievements
Rank 1
answered on 30 Mar 2010, 08:24 PM
This is the actual code.  Can you elaborate on, "attach the handler to the grid control"?
0
robertw102
Top achievements
Rank 1
answered on 30 Mar 2010, 09:04 PM
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.
0
tim
Top achievements
Rank 1
answered on 31 Mar 2010, 02:36 PM
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(); 
    } 

0
tim
Top achievements
Rank 1
answered on 31 Mar 2010, 04:07 PM
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?
0
robertw102
Top achievements
Rank 1
answered on 31 Mar 2010, 07:30 PM
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.
0
tim
Top achievements
Rank 1
answered on 31 Mar 2010, 07:50 PM
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.
0
Pavlina
Telerik team
answered on 01 Apr 2010, 09:56 AM
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.
0
tim
Top achievements
Rank 1
answered on 01 Apr 2010, 01:45 PM
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?
0
robertw102
Top achievements
Rank 1
answered on 01 Apr 2010, 02:52 PM
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.
0
tim
Top achievements
Rank 1
answered on 01 Apr 2010, 02:55 PM
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.
0
Accepted
robertw102
Top achievements
Rank 1
answered on 01 Apr 2010, 03:09 PM
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.
0
tim
Top achievements
Rank 1
answered on 01 Apr 2010, 03:20 PM
This did it, thanks!!
Tags
Grid
Asked by
tim
Top achievements
Rank 1
Answers by
robertw102
Top achievements
Rank 1
tim
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or