C# 筛选BindingSource和DataGridView

C# 筛选BindingSource和DataGridView,c#,.net,datagridview,datasource,C#,.net,Datagridview,Datasource,当我的数据源是BindingSource时,我正在尝试筛选我的DataGridView。 当我尝试在BindingSource中更改筛选器时,只有BindingSource.filter值更改,但DGV看起来仍然相同。如何完成过滤我的DGV BindingSource udalosti = new BindingSource(); //filling bindingsource by udalosti.Add() eventDataGridView.DataSource = udalosti;

当我的数据源是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);
        }
    }
}
数据表:

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);
        }
    }
}
公共类MyDataTable:DataTable { 公共MyDataTable() { 添加(“数据1”,类型(字符串)); 添加(“数据2”,类型(字符串)); 添加(“数据3”,类型(字符串)); 数据行dr; var r=新的随机变量(); 对于(int i=0;i<1000;i++) { dr=NewRow(); dr[“Data1”]=r.Next(111119999); dr[“Data2”]=r.Next(111119999); dr[“Data3”]=r.Next(111119999); 行。添加(dr); } } }
您的数据源是否支持IBindingListView?如果没有,它就不能从BindingSource的过滤功能中获益。文档说它有@HynekBernard没有,它说没有备注摘录:“只有实现
IBindingListView
接口的基础列表支持筛选。”