本文介绍了datagridview绑定源过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从BindingSource中过滤出数据-但它不起作用。 我在做什么错? 我已将我的代码简化为一个简单的示例。
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.
问题是,如果我在文本框中键入内容,则什么也不会发生。
The Problem is, if i type something in the TextBox - nothing happens.
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; } }推荐答案
这对我有用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; } }}
更多推荐
datagridview绑定源过滤器
发布评论