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

How to set the initial SelectedIndex of a Dropdownlist in a RadGrid ItemTemplate

10 Answers 524 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Babu Mannaravalappil
Top achievements
Rank 1
Babu Mannaravalappil asked on 09 Nov 2009, 07:32 PM
Hi,

I have a GridTemplateColumn on a RadGrid.  This Template contains a Label control for ItemTemplate and a DropDownList for the EditItemTemplate.  The label is bound to a field from the DataTable.  Now, When the Grid row goes into Edit mode, I need to set the selectedindex of the dropdown to show the original value from the label.  How and where would I do that?  I don't seem to have access to the Label control from within the ItemDataBound event for the GridEditableItem.  Somebody please help.

Babu.

Here is my Markup:

                    <telerik:GridTemplateColumn DataField="role" HeaderText="Role" UniqueName="Role">  
                        <EditItemTemplate> 
                            <asp:DropDownList ID="ddlRoles" runat="server" DataTextField="Role" DataValueField="Role" 
                                Width="125px">  
                            </asp:DropDownList> 
                        </EditItemTemplate> 
                        <ItemTemplate> 
                            <asp:Label ID="lblRole" runat="server" Text='<%# Bind("role") %>'></asp:Label> 
                        </ItemTemplate> 
                        <ItemStyle Wrap="False" /> 
                    </telerik:GridTemplateColumn> 
 

And here is my code:

    protected void rgUserAdmin_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item.ItemType == GridItemType.EditItem)  
        {  
            GridEditableItem edtItem = (GridEditableItem)e.Item;  
              
            DropDownList ddl = (DropDownList)edtItem.FindControl("ddlRoles");  
            if (null != ddl)  
            {  
                ddl.DataSource = ForecastUtils.GetAllMetricUserRoles();  
                ddl.DataTextField = "role";  
                ddl.DataValueField = "role";  
 
                // this is where I need to set the value of this ddl to the value  
                // from the Label on the ItemTemplate.  
                ddl.SelectedIndex = -1;  
                ddl.DataBind();  
            }  
        }  
    } 

10 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 10 Nov 2009, 07:32 AM
Hello Babu,

You can try out the following code to set the selected value of the dropdownlist to that of the label text in each row:
c#:
protected void rgUserAdmin_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode) 
        { 
            GridEditableItem edtItem = (GridEditableItem)e.Item; 
 
            DropDownList ddl = (DropDownList)edtItem.FindControl("ddlRoles"); 
            if (null != ddl) 
            { 
                ddl.DataSourceID = ForecastUtils.GetAllMetricUserRoles();  
                 
                ddl.SelectedValue = DataBinder.Eval(edtItem.DataItem,"role").ToString(); 
                ddl.DataBind(); 
            } 
        }   

Thanks
Princy.
0
Babu Mannaravalappil
Top achievements
Rank 1
answered on 10 Nov 2009, 12:46 PM
Thanks Princy.  That worked.  But I hoped the old values are persisted by the grid some how (may be in ViewState or something) and I don't have to make a trip to the database to bring them all back.  But I guess that is not so.

Babu.
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 21 Jul 2011, 06:16 AM
Databinder  in code behind?  Who uses that?  Is there any example that is relevant?
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 21 Jul 2011, 06:27 AM
What's the Exact way to set the dropdownlist selected value within the radgrid? (in codebehind or in aspx.  And, I need a 100% complete example, with no missing code, please. )  There's not even one example anywhere?  Isn't this a common thing to do?  If you have a dropdownlist within a grid, the selected value has to be set to the grid's apropriate datasource column.  So, how do we do that? 


ddl.SelectedValue = DataBinder.Eval(edtItem.DataItem,"role").ToString();  
 
Databinder?  Isn't that only for aspx?


Thanks.

0
Shinu
Top achievements
Rank 2
answered on 21 Jul 2011, 06:53 AM
Hello Chris,

You can set the SelectedValue in DropDownList as shown below
aspx:
<asp:DropDownList ID="List" runat="server" SelectedValue='<%# Bind("EmployeeID") %>'>
</asp:DropDownList>

Thanks,
Shinu.
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 21 Jul 2011, 06:17 PM
I understand that already, thanks.  But, if my datasource is a DataSet or DataTable, how exactly would I setup the entire dropdownlist in the rad grid?  I need to set the datasource to a dataset, etc.  I figured out by myself how to do it in codebehind now :

 

 

protected void grdResults_ItemDataBound(object sender, GridItemEventArgs e)

 

{

 

 

if (e.Item is GridDataItem)

 

{

 

 

GridDataItem item = (GridDataItem)e.Item;

 

 

 

DropDownList ddl = (DropDownList)item["tplStatus"].FindControl("ddlStatus");

 

ddl.DataSource =

 

ApplicationState.Current.ApplicationStatus;

 

ddl.DataTextField =

 

"ABBRV_TXT";

 

ddl.DataValueField =

 

"STATUS_CD";

 

ddl.SelectedValue =

 

DataBinder.Eval(item.DataItem, "STATUS_CD").ToString();

 

ddl.DataBind();

}

}

Databinder.eval is in

 

using

 

 

System.Web.UI;

 

  But, I doubt that's the most efficient way to do it??
0
Shinu
Top achievements
Rank 2
answered on 22 Jul 2011, 06:01 AM
Hello Chris,

Check the following help article which explains operations of DropDownList in a TemplateColumn.
Operations with MS DropDownList in EditItemTemplate of GridTemplateColumn.

Thanks,
Shinu.
0
Chris @ Intrinsic
Top achievements
Rank 1
answered on 22 Jul 2011, 06:40 PM
Thanks.  I've already figured out how to do it entirely in codebehind.  But, what about ASPX?  Can I actually set a datasource in ASPX?  I got an error while trying to do that.  But, is there a way?  If so, what is the exact code for that?
0
Steven
Top achievements
Rank 1
answered on 15 Apr 2013, 08:24 PM
I know its been a while but in case someone needs it. 
the Datasource1 its getting information from a different table for the Dropdown. The DataField its the field used in the Grid.
<telerik:GridDropDownColumn DataField="idInGrid" DataSourceID="DataSource1"
                        HeaderText="WhatEver" ListTextField="Name" ListValueField="idName"
                        UniqueName="idDependencias" ColumnEditorID="GridDropDownColumnEditor1">
</telerik:GridDropDownColumn>
0
Llewellen
Top achievements
Rank 1
answered on 16 Sep 2016, 08:42 AM

Hello Shinu,

Your solution worked nicely for me. Thank you.

Regards

Llewellen

Tags
Grid
Asked by
Babu Mannaravalappil
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Babu Mannaravalappil
Top achievements
Rank 1
Chris @ Intrinsic
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Steven
Top achievements
Rank 1
Llewellen
Top achievements
Rank 1
Share this question
or