Filtering BindingSource and DataGridView(筛选BindingSource和DataGridView)
                            本文介绍了筛选BindingSource和DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
当我的数据源为BindingSource时,我正在尝试筛选DataGridView。 当我尝试更改BindingSource中的筛选器时,只有BindingSource.Filter值更改,但DGV看起来仍然相同。如何完成我的DGV过滤?
BindingSource udalosti = new BindingSource();
//filling bindingsource by udalosti.Add()
eventDataGridView.DataSource = udalosti;
这是我的过滤函数
string filter="";
if (typeComboBox.Text != "")
{
    filter = "Typ LIKE '" + typeComboBox.Text + "' AND ";
}
if (descriptionTextBox.Text != "")
{ 
    filter += "Popis LIKE '%" + descriptionTextBox.Text + "%' AND ";
}
if (sourceTextBox.Text != "")
{
    filter += "Zdroj LIKE '" + sourceTextBox.Text + "' AND ";
}
if (filter.Length > 0)
    udalosti.Filter = filter.Substring(0, filter.Length - 5);
else
    udalosti.Filter = filter;
eventDataGridView.ResetBindings();
这就是内容的外观
class Udalost
{
   public string Typ { get; set; }
   public string Popis { get; set; }
   public string Zdroj { get; set; }
   public DateTime Cas { get; set; }
}
推荐答案
使用DataTable是最简单的方法,因此您不必实现IBindingListView。
CS:
public partial class Form1 : Form
{
    MyDataTable dt;
    BindingSource bs;
    public Form1()
    {
        InitializeComponent();
        dt = new MyDataTable();
        bs = new BindingSource();
        bs.DataSource = dt.DefaultView;
        dataGridView1.DataSource = bs;
    }
    private void buttonAdd_Click(object sender, EventArgs e)
    {
        var r = new Random();
        var dr = dt.NewRow();
        dr[0] = r.Next(1111, 9999);
        dr[1] = r.Next(1111, 9999);
        dr[2] = r.Next(1111, 9999);
        dt.Rows.InsertAt(dr, 0);
    }
    private void buttonRemove_Click(object sender, EventArgs e)
    {
        dt.Rows.RemoveAt(0);
    }
    private void buttonClear_Click(object sender, EventArgs e)
    {
        textBox1.Text =
        textBox2.Text =
        textBox3.Text = string.Empty;
        UpdateDgv();
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        UpdateDgv();
    }
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        UpdateDgv();
    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        UpdateDgv();
    }
    private void UpdateDgv()
    {
        string filter = GetFilter();
        if (filter != string.Empty)
            this.Text = filter;
        else this.Text = "All records";
        bs.Filter = filter;
    }
    private string GetFilter()
    {
        string filter = string.Empty;
        if (textBox1.Text != string.Empty)
            filter = string.Format("Data1 like '{0}%'", textBox1.Text);
        if (textBox2.Text != string.Empty)
        {
            if (textBox1.Text != string.Empty)
                filter += " and ";
            filter += string.Format("Data2 like '{0}%'", textBox2.Text);
        }
        if (textBox3.Text != string.Empty)
        {
            if (filter != string.Empty)
                filter += " and ";
            filter += string.Format("Data3 like '{0}%'", textBox3.Text);
        }
        return filter;
    }
}
数据表:
public class MyDataTable : DataTable
{
    public MyDataTable()
    {
        Columns.Add("Data1", typeof(string));
        Columns.Add("Data2", typeof(string));
        Columns.Add("Data3", typeof(string));
        DataRow dr;
        var r = new Random();
        for (int i = 0; i < 1000; i++)
        {
            dr = NewRow();
            dr["Data1"] = r.Next(1111, 9999);
            dr["Data2"] = r.Next(1111, 9999);
            dr["Data3"] = r.Next(1111, 9999);
            Rows.Add(dr);
        }
    }
}
                        这篇关于筛选BindingSource和DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:筛选BindingSource和DataGridView
				
        
 
            
        
             猜你喜欢
        
	     - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 
						
						
						
						
						
				
				
				
				