首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >滤波BindingSource和DataGridView

滤波BindingSource和DataGridView
EN

Stack Overflow用户
提问于 2017-11-29 19:26:22
回答 1查看 2.2K关注 0票数 1

当我的DataGridView是BindingSource时,我正在尝试过滤我的BindingSource。当我试图更改BindingSource中的筛选器时,只有BindingSource.Filter值会改变,但是DGV看起来仍然是一样的。如何完成对DGV的过滤?

代码语言:javascript
复制
BindingSource udalosti = new BindingSource();
//filling bindingsource by udalosti.Add()
eventDataGridView.DataSource = udalosti;

这是我的过滤功能

代码语言:javascript
复制
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();

这就是内容的外观

代码语言:javascript
复制
class Udalost
{
   public string Typ { get; set; }
   public string Popis { get; set; }
   public string Zdroj { get; set; }
   public DateTime Cas { get; set; }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-03 13:08:51

使用DataTable是最简单的方法,所以您不必实现IBindingListView

CS:

代码语言:javascript
复制
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;
    }
}

DataTable:

代码语言:javascript
复制
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);
        }
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47560310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档