This article discusses how to bind a dropdown control in EditItemTemplate with custom values and update the ItemTemplate field after editing. The functionality can be used in cases in which the developer may want to provide different list of choices than the ones present by default in GridDropDownColumn. You should locate the dropdown control in the ItemDataBound handler of the grid for each item which is in edit mode and bind it to the data source of your choice. Furthermore, when update is performed you can save the selected by the user value in Session variable and then set that value for the template column cell in regular mode.The same Session variable can be used to select the default item in the dropdown control on subsequent editing.
In the code below we assign array of ListItem objects as data source for the dropdown editor. To visualize clearly the functionality depicted above we generate only one grid item. For more complex scenarios you should adjust the provided logic to suit your case.
object[] Country_values ={newListItem("Germany","German"),newListItem("England","English"),newListItem("Spain","Spanish"),newListItem("United States","American")};privatevoidPage_Load(object sender,System.EventArgs e){}privatevoidRadGrid1_NeedDataSource(object source,Telerik.Web.UI.GridNeedDataSourceEventArgs e){OleDbConnection MyOleDbConnection =newOleDbConnection(("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("~/Grid/Data/Access/Nwind.mdb")));OleDbDataAdapter MyOleDbDataAdapter =newOleDbDataAdapter();
MyOleDbDataAdapter.SelectCommand =newOleDbCommand("SELECT TOP 1 ContactName, Country FROM Customers", MyOleDbConnection);DataTable myDataTable =newDataTable();
MyOleDbConnection.Open();try{
MyOleDbDataAdapter.Fill(myDataTable);}finally{
MyOleDbConnection.Close();}
RadGrid1.DataSource = myDataTable.DefaultView;}privatevoidRadGrid1_ItemDataBound(object sender,Telerik.Web.UI.GridItemEventArgs e){if(e.Item isGridEditableItem&& e.Item.IsInEditMode){GridEditableItem item = e.Item asGridEditableItem;// access/modify the edit item template settings hereDropDownList list = item.FindControl("List1")asDropDownList;
list.DataSource = Country_values;
list.DataBind();if(Session["updatedValue"]!=null){
list.SelectedValue = Session["updatedValue"];}}elseif(e.Item isGridDataItem&&!e.Item.IsInEditMode && Page.IsPostBack){GridDataItem item = e.Item asGridDataItem;Label label = item.FindControl("Label1")asLabel;// update the label value
label.Text = Session["updatedValue"];}}privatevoidRadGrid1_UpdateCommand(object source,Telerik.Web.UI.GridCommandEventArgs e){GridEditableItem editedItem = e.Item asGridEditableItem;DropDownList list = editedItem.FindControl("List1")asDropDownList;
Session["updatedValue"]= list.SelectedValue;}
VB
Protected Country_values AsObject()={New ListItem("Germany","German"),New ListItem("England","English"),New ListItem("Spain","Spanish"),New ListItem("United States","American")}PrivateSub Page_Load(ByVal sender AsSystem.Object,ByVal e AsSystem.EventArgs)HandlesMyBase.Load
EndSubPrivateSub RadGrid1_NeedDataSource(ByVal source AsSystem.Object,ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)Handles RadGrid1.NeedDataSource
Dim MyOleDbConnection AsNew OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath("~/Grid/Data/Access/Nwind.mdb"))Dim MyOleDbDataAdapter AsNew OleDbDataAdapter
MyOleDbDataAdapter.SelectCommand =New OleDbCommand("SELECT TOP 1 ContactName,Country FROM Customers", MyOleDbConnection)Dim myDataTable AsNew DataTable
MyOleDbConnection.Open()Try
MyOleDbDataAdapter.Fill(myDataTable)Finally
MyOleDbConnection.Close()EndTry
RadGrid1.DataSource = myDataTable.DefaultView
EndSubPrivateSub RadGrid1_ItemDataBound(ByVal sender AsObject,ByVal e As Telerik.Web.UI.GridItemEventArgs)Handles RadGrid1.ItemDataBound
If(TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode)ThenDim item As GridEditableItem = e.Item
'access/modify the edit item template settings hereDim list As DropDownList = item.FindControl("List1")
list.DataSource = Country_values
list.DataBind()If(Not Session("updatedValue")IsNothing)Then
list.SelectedValue = Session("updatedValue")EndIfElseIf(TypeOf e.Item Is GridDataItem AndAlsoNot e.Item.IsInEditMode AndAlso Page.IsPostBack)ThenDim item As GridDataItem = e.Item
Dim label As Label = item.FindControl("Label1")'update the label value
label.Text= Session("updatedValue")EndIfEndSubPrivateSub RadGrid1_UpdateCommand(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridCommandEventArgs)Handles RadGrid1.UpdateCommand
Dim editedItem As GridEditableItem = e.Item
Dim list As DropDownList = editedItem.FindControl("List1")
Session("updatedValue")= list.SelectedValue
EndSub