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:GridDropDownColumnDataSourceID="SqlDataSource1"UniqueName="DropDownColumn1"HeaderText="Country"DataField="ShipCountry"ListDataMember="ShipCountry"ListValueField="ShipCountry"ListTextField="ShipCountry"></telerik:GridDropDownColumn>
The DataSource control
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"SelectCommand="SELECT DISTINCT [ShipCountry] FROM [Orders]"></asp:SqlDataSource>
2. Using GridDropDownColumn with prgrammatic Data Binding:
<telerik:GridDropDownColumnUniqueName="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:
protectedvoidRadGrid1_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:GridTemplateColumnUniqueName="TemplateColumn1"><ItemTemplate><%#Eval("ShipCountry") %></ItemTemplate><EditItemTemplate><telerik:RadDropDownListID="RadDropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="ShipCountry"DataValueField="ShipCountry"SelectedValue='<%# Bind("ShipCountry") %>'></telerik:RadDropDownList></EditItemTemplate></telerik:GridTemplateColumn>
DataSource Control
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"SelectCommand="SELECT DISTINCT [ShipCountry] FROM [Orders]"></asp:SqlDataSource>
4. Using GridTemplateColumn with Programmatic Data Binding
<telerik:GridTemplateColumnUniqueName="TemplateColumn2"><ItemTemplate><%#Eval("ShipCountry") %></ItemTemplate><EditItemTemplate><telerik:RadDropDownListID="RadDropDownList1"runat="server"OnDataBinding="RadDropDownList1_DataBinding"SelectedValue='<%# Bind("ShipCountry") %>'></telerik:RadDropDownList></EditItemTemplate></telerik:GridTemplateColumn>
DataBinding event of the DropDownList
protectedvoidRadDropDownList1_DataBinding(object sender, EventArgs e)
{var ddl = (RadDropDownList)sender;
ddl.DataSource = ComboDataSource();
ddl.DataTextField = "ItemText";
ddl.DataValueField = "ItemId";
}
Kind regards,
Attila Antal
Progress Telerik