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

11 posts, 1 answers
  1. Babu Mannaravalappil
    Babu Mannaravalappil avatar
    15 posts
    Member since:
    Oct 2009

    Posted 09 Nov 2009 Link to this post

    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();  
                }  
            }  
        } 
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Nov 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Babu Mannaravalappil
    Babu Mannaravalappil avatar
    15 posts
    Member since:
    Oct 2009

    Posted 10 Nov 2009 Link to this post

    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.
  5. Chris @ Intrinsic
    Chris @ Intrinsic avatar
    98 posts
    Member since:
    Oct 2010

    Posted 21 Jul 2011 Link to this post

    Databinder  in code behind?  Who uses that?  Is there any example that is relevant?
  6. Chris @ Intrinsic
    Chris @ Intrinsic avatar
    98 posts
    Member since:
    Oct 2010

    Posted 21 Jul 2011 Link to this post

    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.

  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 21 Jul 2011 Link to this post

    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.
  8. Chris @ Intrinsic
    Chris @ Intrinsic avatar
    98 posts
    Member since:
    Oct 2010

    Posted 21 Jul 2011 Link to this post

    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??
  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 22 Jul 2011 Link to this post

    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.
  10. Chris @ Intrinsic
    Chris @ Intrinsic avatar
    98 posts
    Member since:
    Oct 2010

    Posted 22 Jul 2011 Link to this post

    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?
  11. Stendor
    Stendor avatar
    6 posts
    Member since:
    Feb 2013

    Posted 15 Apr 2013 Link to this post

    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>
  12. Llewellen
    Llewellen avatar
    3 posts
    Member since:
    Sep 2016

    Posted 16 Sep in reply to Shinu Link to this post

    Hello Shinu,

    Your solution worked nicely for me. Thank you.

    Regards

    Llewellen

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017