datagridview绑定源过滤器

编程入门 行业动态 更新时间:2024-10-22 23:29:56
本文介绍了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绑定源过滤器

本文发布于:2023-11-09 21:13:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573450.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   绑定   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!