如何在 C# Win Forms 中过滤 DataGridView? [英] How to filter DataGridView in C# Win Forms?

查看:55
本文介绍了如何在 C# Win Forms 中过滤 DataGridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我通过工具箱创建了一个简单的 datagridview,并通过向导(.cs 文件中没有代码)从数据库中选择了数据.它运行完美,如下图所示.

Guys I have created a simple datagridview through toolbox and selected data through wizard (no code in .cs file) from database. It is working flawlessly as you can see in the picture below.

现在我想按联系人姓名过滤其中的条目.我有一个文本框和搜索按钮,因此当用户输入联系人姓名"(例如Altaf")然后单击搜索时,gridview 应该刷新并且只出现ticketid=4 的条目.

Now I want to filter the entries in it by contact person name. I have a textbox and search button so when the user enter a "contact person name" such as "Altaf" and then click on search, the gridview should refresh and only entry with ticketid=4 should appear.

.cs 文件中唯一的代码是:(它是自动生成的)

The only code in the .cs file is : (it was auto-generated)

       private void Form2_Load(object sender, EventArgs e)
    { 
        this.tblTicketDetailTableAdapter.Fill(this.sTDataSet1.tblTicketDetail); //auto-generated
    }

我按照某人的建议在 buttonClick 事件中尝试了此操作,但它生成错误:无法解释位置 27 处的标记 '{'"

I tried this in buttonClick event as suggested by someone but it generates error : "Cannot interpret token '{' at position 27"

        BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView1.DataSource;
        bs.Filter = issuerNameDataGridViewTextBoxColumn + "like '%" + txtbxSearch.Text.Trim().Replace("'", "''") + "%'";
        dataGridView1.DataSource = bs.DataSource;

我在datagridviews或win form编码方面没有经验,所以请详细解释.非常感谢您的帮助.

I have no experience in datagridviews or win form coding for that matter, so please explain in detail. Your help is really appreciated.

问候.

推荐答案

感谢所有回答我问题的人,非常感谢你们的帮助.你们是最有帮助的一群人.

Thank you everyone that has answered to my query, I really appreciate your help guys. You guys are the most helpful bunch.

我通过对代码进行以下修改来解决我的问题:

I have solved my problem by doing following modifications to my code :

    public void btnSearch_Click(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = dataGridView1.DataSource;
        bs.Filter = dataGridView1.Columns[5].HeaderText.ToString() + " LIKE '%" + txtbxSearch.Text + "%'";
        dataGridView1.DataSource = bs;
    }

再次感谢.

这篇关于如何在 C# Win Forms 中过滤 DataGridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆