我可以过滤RepositoryItemLookUpEdit中显示的项目吗?

时间:2013-07-26 19:13:51

标签: c# winforms gridview user-interface devexpress

以下是我的一些代码:

List<Targets> _myList = new List<Targets>();
RepositoryItemLookUpEdit MyRepositoryItemLookUpEdit = new RepositoryItemLookUpEdit();
MyRepositoryItemLookUpEdit.DataSource = _myList;

public class Targets
{
    public string Target { get; set; }
    public bool ShouldDisplay { get; set; }
    public Targets(string target)
    {
        Target = target;
        ShouldDisplay = true;
    }
}

我的问题:是否有可能在显示下拉列表时,只显示带有ShouldDisplay == true的目标?

请注意,事件处理程序可以访问_myList,因此列表中的项目及其ShouldDisplay属性在运行时被修改。例如:

public void MyGrid_CellValueChanging(object sender, CellValueChangedEventArgs e)
{
    if (/* the focused Target item appears more than 3 times in the grid*/)
    { 
        thisTarget.ShouldDisplay = false; // so it will be visually removed from the lookUpEdit and the user cannot select the same one anymore
    }
}
BTW,在CellValueChanging事件处理程序中对DataSource的赋值是不合适的,因为一旦重新分配DataSource,用户所做的任何更改都将被丢弃。

3 个答案:

答案 0 :(得分:1)

更改此行

MyRepositoryItemLookUpEdit.DataSource = new List<Targets>();//i can't get why you are assigning empty list

//your list which is List<Targets> and contains values. Not the empty one like above
MyRepositoryItemLookUpEdit.DataSource = yourList.Where(x=>x.ShouldDisplay ).ToList();

修改

编辑问题后,您需要实施

  

INotifyPropertyChanged

接口。然后每当更改ShouldDisplay的值时,您需要重新绑定。请参阅工作示例here

答案 1 :(得分:0)

编辑:如果它不会阻碍工作流程(如果数据加载足够快以便实时编辑和重新加载),我建议将用户端数据和数据库数据分开。您可以向表中添加一个位字段来存储是否应该显示某个项目,当您从数据库中获取数据时最初显示该数据,然后在进行更改时将ShouldDisplay设置为false,到数据库,从列表中删除它,然后刷新列表。

当然,如果列表中包含大量数据,则此解决方案可能不可行。

原始答案:

Ehsan的建议就是我要发布的内容。另一个选择是循环遍历主目标列表中的项目(无论您将下拉列表数据绑定到何处)并创建一个新列表,如下所示:

List<string> newList = new List<string>();
foreach (Targets t in targetsList)
{
    if (t.ShouldDisplay == true) { newList.Add(t.Target); }
}

然后将下拉列表数据绑定到newList而不是targetList。但是我不确定RepositoryItemLookUpEdit的确切功能,所以这个解决方案可能无效。当你只想显示信息时它很好,但如果你必须与数据本身进行交互可能会更加尴尬。一切都取决于你用它做什么。

答案 2 :(得分:0)

当您使用bindingSource并将RepopsitoryItemLookUpEdit绑定到bindingsource时,您不必重新分配数据源。

public partial class Form1 : Form
{
    private readonly List<Targets> _targetses;
    private BindingList<Targets> _fiList;

    public Form1()
    {
        InitializeComponent();

        this._targetses = new List<Targets>();
        this._targetses.Add(new Targets("Sample"));
        this._targetses.Add(new Targets("Sample"));
        this._targetses.Add(new Targets("Sample"));
        this._targetses.Add(new Targets("Sample") {ShouldDisplay = false});

        this.bindingSource1.DataSource = this.FilteredList;
        this.lookUpEdit1.Properties.DataSource = this.bindingSource1;
        this.bindingSource1.ListChanged += BindingSource1OnListChanged;

        this._targetses[1].ShouldDisplay = false;
    }

    public BindingList<Targets> FilteredList
    {
        get
        {
            return this._fiList ??
                   (this._fiList = new BindingList<Targets>(this._targetses.Where(x => x.ShouldDisplay).ToList()));
        }
    }

    private void BindingSource1OnListChanged(object sender, ListChangedEventArgs listChangedEventArgs)
    {
        this._fiList.Clear();
        foreach (Targets t in this._targetses.Where(x => x.ShouldDisplay)) { this._fiList.Add(t); }
    }

    #region Nested type: Targets

    public class Targets : INotifyPropertyChanged
    {
        private bool _bShouldDisplay;
        private string _sTarget;

        public Targets(string target)
        {
            Target = target;
            ShouldDisplay = true;
        }

        public string Target
        {
            get { return this._sTarget; }
            set
            {
                if (this._sTarget == value)
                    return;

                this._sTarget = value;
                this.OnPropertyChanged();
            }
        }

        public bool ShouldDisplay
        {
            get { return this._bShouldDisplay; }
            set
            {
                if (this._bShouldDisplay == value)
                    return;

                this._bShouldDisplay = value;
                this.OnPropertyChanged();
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

结果:

enter image description here