c# 如何使用 DataSource 隐藏 DataGridView 中的行

标签 c# datagridview hide row visible

我有一个包含许多用户的数据源(BindingList),但有些用户我不想在我的 DataGridView 中显示。是否有可能隐藏它们。我找不到有效的事件。

RowsAdded 有时会隐藏旧行。

最佳答案

看来我必须实现自己的过滤器。我为 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;
    }
}

以下是将新列表绑定(bind)到 DatagridView 的方法:

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

dataGridViewUser.DataSource = userSource;

好吧,Filter 属性还没有什么用处。但 Adapter 类还处于实验阶段。但对于使用 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
}

关于c# 如何使用 DataSource 隐藏 DataGridView 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1457323/

相关文章:

c# - 反射有什么好处?

c# - linq-to-sql 查询结果到 c# 字典 - 如何

asp.net - Gridview 显示编辑按钮

iphone - 检测 UISegmentedControl 的点击部分

css - 隐藏一个 <li> 元素类

jquery - 如何在单击时永久隐藏某个用户的元素

c# - 使用 Fluent NHibernate 的非主键身份自动增量映射

c# - 如何在编辑时在 Windows 应用程序中显示基于组合框选择的控件?

c# - 如何停止数据 GridView 中的闪烁

c# - 单元格焦点上的 CellValidating 错误