第1步:
页面放置AspxGridView控件, 设置列ComboBox1, ComboBox2列, 拟通过ComboBox1列更新联动ComboBox2列.
两个数据列均为"ComboBoxColumn"类型的列.
第2步:客户端源码:
<!--客户端源码--> <dx:GridViewDataComboBoxColumn Caption="ComboBox1" FieldName="box1Column" VisibleIndex="0" Width="200px"> <PropertiesComboBox ValueType="System.String"> /> <dx:GridViewDataComboBoxColumn Caption="ComboBox2" FieldName="box2Column" VisibleIndex="0" Width="200px"> <PropertiesComboBox ValueType="System.String"> <ClientSideEvents SelectedIndexChanged="function(s, e) { onSelectedIndexChanged(s,e); }" /> <script language="javascript" type="text/javascript"> function onSelectedIndexChanged(s, e) { grvList.GetEditor("box2Column").PerformCallback(grvDetailList.GetEditor("box1Column").GetValue()); } </script>
第3步:服务器端源码
//编辑状态控件初始化事件方法
protected void grvList_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) { ASPxGridView grid = sender as ASPxGridView; if (!grid.IsEditing) return; switch (e.Column.FieldName) { //初始化Combox1数据 case "ComboBox1": DevExpress.Web.ASPxEditors.ASPxComboBox box1 = e.Editor as DevExpress.Web.ASPxEditors.ASPxComboBox; DataTable dtBox1 = GetBox1Data(); box1.DataSource = dtBox1; box1.ValueField = "valueField"; box1.TextField = "textField"; box1.DataBind(); box1.SelectedItem = box1.Items.FindByValue(e.Value); break; //初始化box2值 case "ComboBox2": DevExpress.Web.ASPxEditors.ASPxComboBox box2 = e.Editor as DevExpress.Web.ASPxEditors.ASPxComboBox; long box1_value = Convert.ToInt64(grid.GetRowValuesByKeyValue(e.KeyValue, "ComboBox2")); //取当前box1中的值. DataTable dtBox2 = GetBox2Data(box1_value); box2.DataSource = dtBox2; box2.ValueField = "valueField"; box2.TextField = "textField"; box2.DataBind(); box2.SelectedItem = box2.Items.FindByValue(e.Value); //注册ComboBox2 Callback事件,客户端ComboBox1 客户端 SelectItemChanged事件调用 box2.Callback += new DevExpress.Web.ASPxClasses.CallbackEventHandlerBase(grvList_ComboBox2_Callback); break; } }
//ComboBox2 Callback方法 protected void grvList_ComboBox2_Callback(object sender, CallbackEventArgsBase e) { if (string.IsNullOrEmpty(e.Parameter)) return; DevExpress.Web.ASPxEditors.ASPxComboBox box2 = sender as DevExpress.Web.ASPxEditors.ASPxComboBox; DataTable dtBox2 = GetBox2Data(box1_value); box2.DataSource = dtBox2; box2.ValueField = "valueField"; box2.TextField = "textField"; box2.DataBind(); box2.SelectedItem = box.Items.FindByValue(e.Parameter); }
将显示的代码转换成文本形式显示通过这个事件方法.
protected void grvDetailList_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e) { switch (e.Column.FieldName) { case "box1Column": e.DisplayText = GetBox1TextByValue(Convert.ToInt64(e.Value)); break; case "box2Column": e.DisplayText = GetBox2TextByValue(Convert.ToInt64(e.Value)); break; } }