Binding Rad Drop Down List in RadGrid

5 posts, 0 answers
  1. Silviu
    Silviu avatar
    63 posts
    Member since:
    Feb 2015

    Posted 27 Mar 2015 Link to this post

    I have a RadGrid with an update function and an EditItemTemplate.
    In here I have a RadDropDownList which is populated by a SQLDataSource.

    My issue is: when a user clicks Edit on a row and the dropdown is displayed, how do I have the dropdown select the row's value from the list?

    Similar to how the textbox is populated with:

    Text='<%# Bind( "Notes") %>

    What is the equivalent for dropdown?
    My dropdown:
    <telerik:RadDropDownList ID="rddlLocations" runat="server" DataSourceID="GetLocationsSqlDataSource" DataTextField="Location" DataValueField="ID"></telerik:RadDropDownList>

  2. Silviu
    Silviu avatar
    63 posts
    Member since:
    Feb 2015

    Posted 30 Mar 2015 in reply to Silviu Link to this post

    No one?
  3. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 01 Apr 2015 Link to this post

    Hi Silviu,

    Set the SelectedValue property:

    <telerik:RadDropDownList ID="rddlLocations" runat="server" DataSourceID="GetLocationsSqlDataSource"  SelectedValue='<%# Bind("ID") %>'  DataTextField="Location" DataValueField="ID"></telerik:RadDropDownList>

    I hope this helps.

    Regards,
    Hristo Valyavicharski
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  4. Dave
    Dave avatar
    2 posts
    Member since:
    May 2008

    Posted 26 Nov 2018 in reply to Hristo Valyavicharski Link to this post

    I have a similar issue, but when I add the SelectedValue to the RadDropDownlist and run it, I get an error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

    My RadDropDownList is inside an edititemtemplate of a RadDataForm.

    Does the binding to the RadDataForm record have to be done only in code behind?


  5. Attila Antal
    Admin
    Attila Antal avatar
    609 posts

    Posted 28 Nov 2018 Link to this post

    Hi Dave,

    I assume that the RadDropDownList is being bound on the server instead of using Declarative DataSource with DataSourceID property. If that is the case, selecting the item also has to be done on the server. For that you can use the ItemDataBound event of RadGrid, based on the cell's value you can loop through the 

    For example, there is the following GridTemplateColumn with a RadDropDownList in its EditItemTemplate. The DropDownList is wired up to its Load server-side event used for binding data to it.
    <telerik:GridTemplateColumn UniqueName="TemplateColumn1" HeaderText="Template Column">
        <ItemTemplate>
            <%# Eval("ShipName") %>
        </ItemTemplate>
        <EditItemTemplate>
            <telerik:RadDropDownList ID="RadDropDownList1" runat="server" OnLoad="RadDropDownList1_Load"></telerik:RadDropDownList>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>

    C# RadDropDownList Load event handler:
    protected void RadDropDownList1_Load(object sender, EventArgs e)
    {
        RadDropDownList rddl = sender as RadDropDownList;
        rddl.DataSource = OrdersTable().DefaultView.ToTable(true, "ShipName");
        rddl.DataBind();
        rddl.DataTextField = "ShipName";
        rddl.DataValueField = "ShipName";
    }

    Finally, in the ItemDataBound event handler of the RadGrid, you can get a reference to the RadDropDownList control, find the DropDownItem with the same value as the one bound to the cell and make it selected.
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode)
        {
            GridEditableItem editItem = e.Item as GridEditableItem;
            DataRowView dataRow = e.Item.DataItem as DataRowView;
     
            RadDropDownList rddl = editItem["TemplateColumn1"].FindControl("RadDropDownList1") as RadDropDownList;
     
            DropDownListItem ddlItem = rddl.FindItemByValue(dataRow["ShipName"].ToString()) as DropDownListItem;
     
            if (ddlItem != null)
            {
                ddlItem.Selected = true;
            }
        }
     
        // access the data item
        if(e.Item is GridDataItem && !e.Item.IsInEditMode)
        {
            // cast e.Item to GridDataItem type
            GridDataItem dataItem = e.Item as GridDataItem;
            // get/set the ShipName cell's value to replaced values
            dataItem["ShipName"].Text = dataItem["ShipName"].Text.Replace(",", ", ");
        }
    }

    Related articles:
    I hope this will help resolve the issue.

    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.

Back to Top