C# – How to filter devexpress lookupedit datasource in gridview

cdevexpress

I have a DevExpress gridview with 4 columns.
The first column is a Repository LookUpEdit with some data let say with CarTypes for examples.
The second column is a Repository LookUpEdit with Cars.
The third is a EditBox repository with user comments
and the last column is a simple button to delete the row.

I want to filter the second column data per row, when change the value of the first column. For example when choose SUV the I want to show in the second column repository only cars with CarType = SUV.

Can someone help me to resolve this problem? Thank you

Best Answer

It is just simple to implement.. As for different values for different rows in the combo box, to introduce this functionality you can dynamically filter the underlying lookup data source after the editor is displayed. This should be done by handling the ShownEditor event.

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.DataSource as DataTable;
        clone = new DataView(table);
        DataRow row = view.GetDataRow(view.FocusedRowHandle);
        clone.RowFilter = "[CountryCode] = " + row["CountryCode"].ToString();
        edit.Properties.DataSource = clone;
    }
}

private void gridView1_HiddenEditor(object sender, System.EventArgs e) {
    if (clone != null) {
        clone.Dispose();
        clone = null;
    }
}

Please review the How to filter a second LookUp column based on a first LookUp column's value article that describes this approach in greater detail.

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

Related Topic