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

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

问题描述

我已经通过工具箱创建了一个简单的datagridview,并通过向导(.cs文件中没有代码)从数据库中选择数据。它正在正常工作,如下图所示。



现在我想通过联系人姓名过滤其中的条目。我有一个文本框和搜索按钮,所以当用户输入联系人姓名,如Altaf,然后点击搜索,gridview应该刷新,只有entryid = 4应该出现。



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

  private void Form2_Load(object sender,EventArgs e)
{
this.tblTicketDetailTableAdapter.Fill(this.sTDataSet1.tblTicketDetail); //自动生成
}

我在buttonClick事件中尝试过这个,它会产生错误:无法解释令牌{'在位置27

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

我没有datagridviews或winform编码的经验,所以请详细说明。您的帮助非常感激。



问候。

解决方案

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



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

  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;
}

再次感谢。


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.

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.

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
    }

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;

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

Regards.

解决方案

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;
    }

Thank you again.

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

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