Description:
How do I dynamically assign values to the combo box inplace editor based on another column value?
Answer:
Applies to:
XtraGrid, XtraTreeList, XtraVerticalGrid
You should handle the GridView.ShownEditor event, obtain the current in-place editor via the ActiveEditor property, typecast it to ComboBoxEdit and populate its Items property with the desired elements depending on the GridView.FocusedRowHandle value.
C#using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
Hashtable units = new Hashtable();
private void gridView1_ShownEditor(object sender, System.EventArgs e) {
GridView view = sender as GridView;
if(view.FocusedColumn.FieldName == "Unit") {
ComboBoxEdit edit = (ComboBoxEdit)view.ActiveEditor;
edit.Properties.Items.Clear();
object parameter = view.GetRowCellValue(view.FocusedRowHandle, "Parameter");
if(units[parameter] != null)
edit.Properties.Items.AddRange((ICollection)units[parameter]);
}
}
Visual BasicImports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Grid
Dim units As New Hashtable
Private Sub GridView1_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.ShownEditor
Dim view As GridView = CType(sender, GridView)
If view.FocusedColumn.FieldName = "Unit" Then
Dim edit As ComboBoxEdit = CType(view.ActiveEditor, ComboBoxEdit)
edit.Properties.Items.Clear()
Dim parameter As Object = view.GetRowCellValue(view.FocusedRowHandle, "Parameter")
If Not units(parameter) Is Nothing Then
edit.Properties.Items.AddRange(CType(units(parameter), ICollection))
End If
End If
End Sub
See also:
DevExpress WinForms Cheat Sheet - Filter DevExpress Data-Aware Controls
How to show different editors in the same column
How to filter a second LookUp column based on a first LookUp column's value
Assigning Editors to Individual Cells
GridView.CustomRowCellEditForEditing Event