C# datagridview 绑定源过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17309270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:07:05  来源:igfitidea点击:

datagridview binding source filter

c#datagridviewfilterbindingsource

提问by Blindsurfer

I am trying to filter Data out of a BindingSource - but it doesnt work. What am i doing wrong? I have reduced my Code to a minimalistic example.

我正在尝试从 BindingSource 中过滤数据 - 但它不起作用。我究竟做错了什么?我已将我的代码简化为一个简约的示例。

The Problem is, if i type something in the TextBox - nothing happens.

问题是,如果我在 TextBox 中输入一些东西 - 什么也没有发生。

public partial class Form1 : Form
{
    BindingSource bs = new BindingSource();

    public Form1()
    {
        InitializeComponent();
        List<myObj> myObjList= new List<myObj>();
        myObjList.Add(new myObj("LastNameA", "Peter"));
        myObjList.Add(new myObj("LastNameA", "Klaus"));
        myObjList.Add(new myObj("LastNameB", "Peter"));

        foreach (myObj obj in myObjList)
        {
            bs.Add(obj);
        }
        dataGridView1.DataSource = bs;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        bs.Filter = string.Format("Name LIKE '%{0}%'", textBox1.Text);
        dataGridView1.Refresh();
    }

}

public class myObj
{
    public myObj(string LastName, String Name)
    {
        this.LastName = LastName;
        this.Name = Name;
    }

    public string LastName { get; set; }
    public string Name { get; set; }
}

采纳答案by Blindsurfer

This Worked for me so far

到目前为止这对我有用

public partial class Form1 : Form
{
    BindingSource bs = new BindingSource();
    BindingList<myObj> myObjList = new BindingList<myObj>();

    public Form1()
    {
        InitializeComponent();

        myObjList.Add(new myObj("LastNameA", "Peter"));
        myObjList.Add(new myObj("LastNameA", "Klaus"));
        myObjList.Add(new myObj("LastNameB", "Peter"));

        bs.DataSource = myObjList;

        dataGridView1.DataSource = myObjList;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        BindingList<myObj> filtered = new BindingList<myObj>(myObjList.Where(obj => obj.Name.Contains(textBox1.Text)).ToList());

        dataGridView1.DataSource = filtered;
        dataGridView1.Update();
    }

}

public class myObj
{
    public myObj(string LastName, String Name)
    {
        this.LastName = LastName;
        this.Name = Name;
    }

    public string LastName { get; set; }
    public string Name { get; set; }
}

}

}

回答by Nikola Mitev

The MSDN Documentationsays:

MSDN文档说:

Only underlying lists that implement the IBindingListView interface support filtering.

只有实现 IBindingListView 接口的底层列表才支持过滤。

Replace this

替换这个

 List<myObj> myObjList= new List<myObj>();

with this

有了这个

 BindingList<myObj> myObjList= new BindingList<myObj>();