Devexpress GridControl中combobox级联显示
的有关信息介绍如下:
在使用GridControl时,可能会有需求要求某2列显示combobox控件,而且在选择第一列的值时,第2列绑定的数据源发生变化。当然这在其他地方很容易实现,但是在gridcontrol的列中就不能用以往的思维方式进行了,因为在gridcontrol中,你只有选中这一列,它才会显示出该列所绑定控件的特性,否则只是一个普通的lable。
基本思路:在点击第2列时才去获取第一列选中的值,然后根据该值查询出第2列的数据进行绑定。
combobox控件的创建,我是在CustomRowCellEditForEditing这个事件下处理的。(因为我所需要显示combobox的列是动态创建的,所以需要这样创建,如果你是固定显示,直接绑定combobox点击事件就可以了。)在这个事件里面,还可以控制某一列不同行显示不同控件。(因为Devexpress是只能设置某一列控件的属性的,不能精准到控制每个单元格中的控件)
private void gvwFetchData_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView; RepositoryItemComboBox cbx = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox(); switch (e.Column.FieldName) { //case "DBConstructionID": //cbx.Items.Clear(); // cbx.Items.AddRange(cbxConstructions.Select(c => new Models.ComboBoxItemTextValue
//获取该行楼盘id string constructid = gvwFetchData.GetRowCellValue(e.RowHandle, "DBConstructionID").ToString(); //楼栋
//根据楼盘查询楼栋的数据 IList
//给该行楼栋combobox绑定数据源 cbx.Items.Clear(); cbx.Items.AddRange(cbxBuildings.Select(b => new Models.ComboBoxItemTextValue
//combobox值改变后触发 cbx.SelectedValueChanged += new EventHandler(cbx_SelectedValueChanged);
//下拉框选中值后,需要进行转换 cbx.ParseEditValue += new ConvertEditValueEventHandler(cbx_ParseEditValue);
//指定该列控件 e.RepositoryItem = cbx; break; case "HouseName": string BuindId = gvwFetchData.GetRowCellValue(e.RowHandle, "DBBuildingID").ToString(); //房号 IList
选择combobox的值时进行的处理。(例如选择楼盘后,将楼盘id存放在一隐藏列中,在点击楼栋时,获取该id,查询所需要的数据源,进行绑定)
private void cbx_SelectedValueChanged(object sender, EventArgs e) { BaseEdit edit = gvwFetchData.ActiveEditor; //下拉框数据更改之后,改变级联下拉框数据 switch (gvwFetchData.FocusedColumn.FieldName) { //case "ConstructionName"://楼盘 // gvwFetchData.SetFocusedRowCellValue(gvwFetchData.Columns["DBConstructionID"], ((ComboBoxItemTextValue
}
//grid中的下拉框必须要处理一下,不然会报(对象必须实现iconvertible)错误 private void cbx_ParseEditValue(object sender, ConvertEditValueEventArgs e) { e.Value = e.Value.ToString(); e.Handled = true; }



