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

Binding Rad Drop Down List in RadGrid

4 Answers 1092 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Silviu
Top achievements
Rank 1
Silviu asked on 27 Mar 2015, 12:53 PM
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>

4 Answers, 1 is accepted

Sort by
0
Silviu
Top achievements
Rank 1
answered on 30 Mar 2015, 07:19 AM
No one?
0
Hristo Valyavicharski
Telerik team
answered on 01 Apr 2015, 08:05 AM
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.

 
0
Dave
Top achievements
Rank 2
answered on 26 Nov 2018, 03:35 PM

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?


0
Attila Antal
Telerik team
answered on 28 Nov 2018, 02:21 PM
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.

Tags
DropDownList
Asked by
Silviu
Top achievements
Rank 1
Answers by
Silviu
Top achievements
Rank 1
Hristo Valyavicharski
Telerik team
Dave
Top achievements
Rank 2
Attila Antal
Telerik team
Share this question
or