如何在gridview中过滤devexpress lookupedit数据源

时间:2015-12-01 11:17:30

标签: c# devexpress

我有一个包含4列的DevExpress gridview。 第一列是Repository LookUpEdit,其中包含一些数据,例如CarTypes。 第二列是带有Cars的Repository LookUpEdit。 第三个是带有用户注释的EditBox存储库 最后一列是一个删除行的简单按钮。

我想在更改第一列的值时,每行过滤第二列数据。例如,当选择SUV时,我想在第二列存储库中仅显示CarType = SUV的汽车。

有人可以帮我解决这个问题吗?谢谢

2 个答案:

答案 0 :(得分:3)

实现起来很简单..对于组合框中不同行的不同值,为了引入此功能,您可以在显示编辑器后动态过滤基础查找数据源。这应该通过处理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;
    }
}

请查看更详细地介绍此方法的How to filter a second LookUp column based on a first LookUp column's value文章。

参考文献:
How to: Filter a LookUp(ComboBox) Column Based on Another Column Value

答案 1 :(得分:0)

我发现来自https://www.devexpress.com/Support/Center/Question/Details/A237的轰鸣声代码并且完美无缺!

我使用了@NiranjanKala提出的ShownEditor事件。检查编辑的列是否正确,然后使用新数据源重新加载repository lookUpEdit datasource

private void myCarsGridView_ShownEditor(object sender, EventArgs e)
{
    try
    {
        ColumnView view = (ColumnView)sender;
        if (view.FocusedColumn.FieldName == "CarType.Id" && view.ActiveEditor is LookUpEdit)
        {
            LookUpEdit edit = (LookUpEdit)view.ActiveEditor;
            int carTypeId = (int)view.GetFocusedRowCellValue("CarType.Id");
            IList<Car> filteredCars = _controller.GetCarsByType(carTypeId);
            edit.Properties.DataSource = filteredCars;
        }
    }
    catch (System.Exception ex)
    {
        //Log
    }

}