Hi Lenard,
In general there are three ways to bind data to RadGrid or its controls. One of those is binding using Declarative DataSource, binding on the code behind programmatically or binding on client using JavaScript objects or WebServices, see Telerik RadGrid Data Binding Basics
Here are few examples to have DropDown in RadGrid and bind data to it
1. Using GridDropDownColumn with Declarative DataSource
<telerik:GridDropDownColumn
DataSourceID="SqlDataSource1"
UniqueName="DropDownColumn1"
HeaderText="Country"
DataField="ShipCountry"
ListDataMember="ShipCountry"
ListValueField="ShipCountry"
ListTextField="ShipCountry">
</telerik:GridDropDownColumn>
The DataSource control
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [ShipCountry] FROM [Orders]">
</asp:SqlDataSource>
2. Using GridDropDownColumn with prgrammatic Data Binding:
<telerik:GridDropDownColumn
UniqueName="DropDownColumn2"
HeaderText="Country"
DataField="CountryID"
ListDataMember="CountryID"
ListValueField="CountryID"
ListTextField="ShipCountry">
</telerik:GridDropDownColumn>
In the ItemDataBound event of RadGrid access the Combo and assign it a DataSource:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.IsInEditMode)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
RadComboBox combo = editedItem["DropDownColumn2"].Controls[0] as RadComboBox;
combo.DataSource = ComboDataSource();
combo.DataTextField = "ItemText";
combo.DataValueField = "ItemId";
combo.DataBind();
var CountryID = editedItem.GetDataKeyValue("CountryID");
var selectedItem = combo.FindItemByValue(CountryID.ToString());
if (selectedItem != null)
{
selectedItem.Selected = true;
}
}
}
3. Using GridTemplateColumn with Declarative DataSource Control
<telerik:GridTemplateColumn UniqueName="TemplateColumn1">
<ItemTemplate>
<%# Eval("ShipCountry") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList ID="RadDropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="ShipCountry"
DataValueField="ShipCountry"
SelectedValue='<%# Bind("ShipCountry") %>'>
</telerik:RadDropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
DataSource Control
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT DISTINCT [ShipCountry] FROM [Orders]">
</asp:SqlDataSource>
4. Using GridTemplateColumn with Programmatic Data Binding
<telerik:GridTemplateColumn UniqueName="TemplateColumn2">
<ItemTemplate>
<%# Eval("ShipCountry") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDropDownList ID="RadDropDownList1" runat="server"
OnDataBinding="RadDropDownList1_DataBinding"
SelectedValue='<%# Bind("ShipCountry") %>'></telerik:RadDropDownList>
</EditItemTemplate>
</telerik:GridTemplateColumn>
DataBinding event of the DropDownList
protected void RadDropDownList1_DataBinding(object sender, EventArgs e)
{
var ddl = (RadDropDownList)sender;
ddl.DataSource = ComboDataSource();
ddl.DataTextField = "ItemText";
ddl.DataValueField = "ItemId";
}
Kind regards,
Attila Antal
Progress Telerik
Get
quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers.
Learn More.