datagridview绑定源过滤器 [英] datagridview binding source filter
本文介绍了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绑定源过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文