문제

많은 사용자와 DataSource (BindingList)가 있지만 DataGridView에 표시하고 싶지 않은 일부 사용자가 있습니다. 그들을 숨길 수 있습니까? 작동하는 이벤트를 찾을 수 없습니다.

줄이 닿는 행은 때때로 worng 행을 숨 깁니다.

도움이 되었습니까?

해결책

내 자신의 필터를 구현 해야하는 것 같습니다. Bindinglist에 대한 어댑터를 작성하여 모든 Bindinglist의 필터링 버전을 표시 할 수 있습니다. 당신은 단지 그것으로부터 온화해야합니다. 여기 내 예입니다. user.canedit = true로 사용자 만 보여주고 싶습니다.

public class AhpUserFilter : FilterBindingListAdapter<AhpUser>
{

    public AhpUserFilter(AhpUserCollection users)
        : base(users.GetList() as IBindingList)
    {

    }

    protected override bool ISVisible(AhpUser user)
    {
        return user.CanEdit;
    }
}

새 목록을 DataGridView에 바인딩하는 방법은 다음과 같습니다.

AhpUserFilter userSource = new AhpUserFilter(users);
userSource.Filter = "yes!";

dataGridViewUser.DataSource = userSource;

좋아, 필터 속성은 아직 쓸모가 없다. 그러나 어댑터 클래스는 아직 실험적입니다. 그러나 DataGrid로 간단한 추가 및 제거하려면 잘 작동하는 것 같습니다.

어댑터의 코드는 다음과 같습니다.

public class FilterBindingListAdapter<T> : BindingList<T>, IBindingListView
{
    protected string filter = String.Empty;
    protected IBindingList bindingList;
    private bool filtering = false;

    public FilterBindingListAdapter(IBindingList bindingList)
    {
        this.bindingList = bindingList;
        DoFilter();
    }


    protected override void OnListChanged(ListChangedEventArgs e)
    {
        if (!filtering)
        {
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    bindingList.Insert(e.NewIndex, this[e.NewIndex]);
                    break;
            }
        }

        base.OnListChanged(e);
    }

    protected override void RemoveItem(int index)
    {
        if (!filtering)
        {
            bindingList.RemoveAt(index);
        }

        base.RemoveItem(index);
    }

    protected virtual void DoFilter()
    {
        filtering = true;
        this.Clear();

        foreach (T e in bindingList)
        {
            if (filter.Length == 0 || this.ISVisible(e))
            {
                this.Add((T)e);
            }
        }
        filtering = false;
    }

    protected virtual bool ISVisible(T element)
    {
        return true;
    }


    #region IBindingListView Members

    public void ApplySort(ListSortDescriptionCollection sorts)
    {
        throw new NotImplementedException();
    }

    public string Filter
    {
        get
        {
            return filter;
        }
        set
        {
            filter = value;
            DoFilter();
        }
    }

    public void RemoveFilter()
    {
        Filter = String.Empty;
    }

    public ListSortDescriptionCollection SortDescriptions
    {
        get { throw new NotImplementedException(); }
    }

    public bool SupportsAdvancedSorting
    {
        get { return false; }
    }

    public bool SupportsFiltering
    {
        get { return true; }
    }

    #endregion
}

다른 팁

당신은 줄을 필터링 할 수 있습니다 BindingSource.Filter 재산. 그러나 내장 구현 BindingList<T> 필터링을 지원하지 않으므로 직접 구현해야합니다. Google에서 몇 가지 예를 찾을 수 있습니다. 이 하나 흥미로워 보입니다 ...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top