HOME PAGE | DOWNLOAD | TUTORIALS | XtraReports
Devexpress

Sunday, July 22, 2012

How to: Filter a LookUp(ComboBox) Column Based on Another Column Value

Assume that a grid column's in-place editor provides a drop-down list of specific values, and the values must be filtered according to other row values. For instance, a grid contains two columns: the first column is used to edit a Country field, while the second column should provide a list of cities for the selected country. To display the appropriate values only, the second column's values should be filtered based on the first column's value.

Use a LookUpEdit or ComboBoxEdit in-place editor for a column whose drop-down values need to be filtered. When using a LookUpEdit editor, you can assign a list of items to be displayed in the drop-down directly to the editor's DataSource property. A ComboBoxEdit control doesn't provide this property. The drop-down list should be populated manually by adding items to the editor's Items collection.

To filter the column's drop-down values, handle the ColumnView.ShownEditor event. In this event, the active editor's drop-down list must be filtered as required. See the following code for an example.

To get the full source code of the example, refer to the following Knowledge Base article:

How to filter a second LookUp column based on a first LookUp column's value

C#:
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
 
private DataView clone = null;
 
private void gridView1_ShownEditor(object sender, System.EventArgs e) {
    GridView view = sender as GridView;
    if (view.FocusedColumn.FieldName == "CityCode" && view.ActiveEditor is LookUpEdit) {
        Text = view.ActiveEditor.Parent.Name;
        DevExpress.XtraEditors.LookUpEdit edit;
        edit = (LookUpEdit)view.ActiveEditor;
 
        DataTable table = edit.Properties.LookUpData.DataSource as DataTable;
        clone = new DataView(table);
        DataRow row = view.GetDataRow(view.FocusedRowHandle);
        clone.RowFilter = "[CountryCode] = " + row["CountryCode"].ToString();
        edit.Properties.LookUpData.DataSource = clone;
    }
}
 
private void gridView1_HiddenEditor(object sender, System.EventArgs e) {
    if (clone != null) {
        clone.Dispose();
        clone = null;
    }
}
VB:
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Grid
 
Private clone As DataView
 
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 = "CityCode" AndAlso _
      TypeOf view.ActiveEditor Is LookUpEdit Then
        Dim edit As LookUpEdit
        Dim table As DataTable
        Dim row As DataRow
 
        edit = CType(view.ActiveEditor, .LookUpEdit)
 
        table = CType(edit.Properties.DataSource, DataTable)
        clone = New DataView(table)
        row = view.GetDataRow(view.FocusedRowHandle)
        clone.RowFilter = "[CountryCode] = " + row("CountryCode").ToString()
        edit.Properties.DataSource = clone
    End If
End Sub
 
Private Sub GridView1_HiddenEditor(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles GridView1.HiddenEditor
    If Not clone Is Nothing Then
        clone.Dispose()
        clone = Nothing
    End If
End Sub

 

No comments:

Post a Comment