0

这是我的一些代码:

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

顺便说一句,在 CellValueChanging 事件处理程序中分配给 DataSource 是不合适的,因为一旦重新分配 DataSource,用户所做的任何更改都将被丢弃。

4

3 回答 3

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 的值时,您都需要重新绑定。在此处查看一个工作示例

于 2013-07-26T19:38:52.020 回答
0

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

当然,由于列表中有大量数据,这种解决方案可能不可行。

原答案:

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

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

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

于 2013-07-26T19:59:08.023 回答
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
}

结果:

在此处输入图像描述

于 2013-07-26T22:05:43.267 回答