【问题标题】:How to filter devexpress lookupedit datasource in gridview如何在gridview中过滤devexpress lookupedit数据源
【发布时间】:2015-12-01 11:17:30
【问题描述】:

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

我想在更改第一列的值时过滤每行的第二列数据。 For example when choose SUV the I want to show in the second column repository only cars with CarType = SUV.

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

【问题讨论】:

    标签: c# devexpress


    【解决方案1】:

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

    【讨论】:

    • 您没有提供太多细节,也没有展示任何实现。这个示例代码只是为了实现的想法。很高兴你已经解决了你的问题。享受.. 网格控件具有不同的视图,因此需要在特定视图中投射..
    【解决方案2】:

    我从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
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 2013-11-23
      • 2014-11-28
      • 2014-02-19
      相关资源
      最近更新 更多