在datagridview中过滤数据C#

我试图过滤掉我想要的数据,并隐藏所有其他数据.
 这是我的代码清单,

private void searchButton_Click_1(object sender, EventArgs e)
        {

            BindingSource bs = new BindingSource();
            bs.DataSource = dataGridView1.DataSource;
            bs.Filter = dataGridView1.Columns[1].HeaderText.ToString() + " LIKE '%" +  searchTextBox.Text + "%'";
            dataGridView1.DataSource = bs;

        }

但是当我运行它时,它提示我错误信息

“在’ID’运算符之后缺少操作数.”

而且我也尝试过这个,同样的错误信息.

private void searchButton_Click_1(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dataGridView1.DataSource;
           // bs.Filter = dataGridView1.Columns[1].HeaderText.ToString() + " LIKE '%" + searchTextBox.Text + "%'";
            bs.Filter = "Sample ID like '*" + searchTextBox.Text + "*'";
            dataGridView1.DataSource = bs;
        }

谁能帮我澄清一下问题吗?谢谢.

解决方法:

尝试以下,

BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "yourColumnName like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;

要么

在“搜索文本框已更改”事件中,尝试以下操作,

(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", searchTextBox.Text);
上一篇:清除BindingList并添加新项目很慢?


下一篇:c#-自定义自动生成的类的显示