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:
What is the equivalent for dropdown?
My dropdown:
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
0
Silviu
Top achievements
Rank 1
answered on 30 Mar 2015, 07:19 AM
No one?
0
Hi Silviu,
Set the SelectedValue property:
I hope this helps.
Regards,
Hristo Valyavicharski
Telerik
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
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.
C# RadDropDownList Load event handler:
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.
Related articles:
I hope this will help resolve the issue.
Kind regards,
Attila Antal
Progress Telerik
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:
- Accessing Cells and Rows
- GridItem type for edit mode
- Telerik RadGrid Data Binding Basics
- RadDropDownList - Data binding, to DataTable
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.