In many situations you may need to get reference to controls in grid row/edit form and modify their state depending on the option the user chooses for another control in the same row/edit form. You can reference the grid item which wraps the controls (either data item or edit form item), then locate the other control and modify its state as per your custom needs. In some cases (when having additional containers in the rows), you may need to find the container first and then propagate the same steps (depicted above).
The upcoming code sample represents how to alter the color for the City column on checkbox state change and how to load different set of items for combo in par with the user selection in another combo. Please note that the items refresh action is not included:
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 5 CustomerID, ContactName, ContactTitle, City FROM Customers", MyOleDbConnection);DataTable myDataTable =newDataTable();
MyOleDbConnection.Open();try{
MyOleDbDataAdapter.Fill(myDataTable);}finally{
MyOleDbConnection.Close();}
RadGrid1.DataSource = myDataTable.DefaultView;}privatevoidRadGrid1_ItemCreated(object sender,Telerik.Web.UI.GridItemEventArgs e){if(e.Item isGridEditableItem&& e.Item.IsInEditMode){//the dropdown list will be the first control in the Controls collection of the corresponding cell DropDownList list =(e.Item asGridEditableItem)["ddlContactName"].Controls[0]asDropDownList;//attach SelectedIndexChanged event for the drodown control
list.AutoPostBack =true;
list.SelectedIndexChanged +=newSystem.EventHandler(this.list_SelectedIndexChanged);}}privatevoidlist_SelectedIndexChanged(object sender,System.EventArgs e){//first reference the edited grid item through the NamingContainer attributeGridEditableItem editedItem =(sender asDropDownList).NamingContainer asGridEditableItem;//the dropdown list will be the first control in the Controls collection of the corresponding cell //for custom edit forms (WebUserControl/FormTemplate) you can find the column editor with the FindControl(controlId) method DropDownList ddList = editedItem["ddlContactTitle"].Controls[0]asDropDownList;// change the data source for ContactTitle with custom code here DataTable table = DataSourceHelperCS.GetDataTable("SELECT CustomerID, ContactTitle FROM Customers WHERE ContactName = '"+(editedItem["ddlContactName"].Controls[0]asDropDownList).SelectedItem.Text +"'");
ddList.DataSource = table;
ddList.DataBind();
RadGrid1.Controls.Add(newLiteralControl("<b>the available options for Contact Title has been changed</b>"));}protectedvoidCheckedChanged(object sender,System.EventArgs e){CheckBox chkBox =(sender asCheckBox);//find the City field in the row and change its color//first reference the panel which wraps the checkbox control through the Parent property Panel myPanel = chkBox.Parent asPanel;//then fetch the grid data item through the NamingContainer attribute of the container control (panel in this case) GridDataItem dataItem = myPanel.NamingContainer asGridDataItem;//finally modify the color for the City field according to the checkbox status if(chkBox.Checked){
dataItem["City"].Style["color"]="red";}else{
dataItem["City"].Style["color"]="black";}}
VB
PrivateSub RadGrid1_NeedDataSource(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)Handles RadGrid1.NeedDataSource
Dim MyOleDbConnection As OleDbConnection =New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="+ Server.MapPath
("~/Grid/Data/Access/Nwind.mdb"))Dim MyOleDbDataAdapter As OleDbDataAdapter =New OleDbDataAdapter
MyOleDbDataAdapter.SelectCommand =New OleDbCommand("SELECT TOP 5 CustomerID, ContactName, ContactTitle, City FROM Customers",
MyOleDbConnection)Dim myDataTable As DataTable =New DataTable
MyOleDbConnection.Open()Try
MyOleDbDataAdapter.Fill(myDataTable)Finally
MyOleDbConnection.Close()EndTry
RadGrid1.DataSource = myDataTable.DefaultView
EndSubPrivateSub RadGrid1_ItemCreated(ByVal sender AsObject,ByVal e As Telerik.Web.UI.GridItemEventArgs)Handles RadGrid1.ItemCreated
If(TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode)Then'the dropdown list will be the first control in the Controls collection of the corresponding cellDim list As DropDownList =CType(CType(e.Item, GridEditableItem)("ddlContactName").Controls(0), DropDownList)'attach SelectedIndexChanged event for the drodown control
list.AutoPostBack =TrueAddHandler list.SelectedIndexChanged,AddressOfMe.list_SelectedIndexChanged
EndIfEndSubPrivateSub list_SelectedIndexChanged(ByVal sender AsObject,ByVal e AsSystem.EventArgs)'first reference the edited grid item through the NamingContainer attributeDim editedItem As GridEditableItem =CType(CType(sender, DropDownList).NamingContainer, GridEditableItem)'the dropdown list will be the first control in the Controls collection of the corresponding cell'for custom edit forms (WebUserControl/FormTemplate) you can find the column editor with the FindControl(controlId) methodDim ddList As DropDownList =CType(editedItem("ddlContactTitle").Controls(0), DropDownList)' change the data source for ContactTitle with custom code hereDim table As DataTable = DataSourceHelperVB.GetDataTable(" SELECT CustomerID, ContactTitle FROM Customers WHERE ContactName = '"&CType(editedItem("ddlContactName").Controls(0), DropDownList).SelectedItem.Text&"'")
ddList.ClearSelection()
ddList.DataSource = table
ddList.DataBind()
RadGrid1.Controls.Add(New LiteralControl("<b>the available options for Contact Title has been changed</b>"))EndSubProtectedSub CheckedChanged(ByVal sender AsObject,ByVal e AsSystem.EventArgs)Dim chkBox As CheckBox =CType(sender, CheckBox)'find the City field in the row and change its color'first reference the panel which wraps the checkbox control through the Parent propertyDim myPanel As Panel =CType(chkBox.Parent, Panel)'then fetch the grid data item through the NamingContainer attribute of the container control (panel in this case)Dim dataItem As GridDataItem =CType(myPanel.NamingContainer, GridDataItem)'finally modify the color for the City field according to the checkbox statusIf chkBox.Checked Then
dataItem("City").Style("color")="red"Else
dataItem("City").Style("color")="black"EndIfEndSub