4

我正在尝试过滤掉我想要的数据,并隐藏所有其他数据。这是我的代码列表,

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

谁能帮我澄清这个问题?谢谢。

4

5 回答 5

5

尝试以下

BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "[HeaderText] Like '%" + searchTextBox.Text + "%'";
dataGridView1.DataSource = bs;
于 2014-06-13T02:44:42.553 回答
1

我将数据源设置为列表并使用 LINQ 过滤 datagridview。

//Declare Global
List<ProductList> _productList

//Somewhere in code initialize datagridview
DataGridView1.DataSource = _productList

//TextBox TextChanged Function
private void TxtSearchProduct_TextChanged(object sender, EventArgs e)
{
   var result = _productList.Where(x => 
   x.ProductName.Contains(TxtSearchProduct.Text)).ToList();
   DataGridView1.DataSource = result;
}

我希望它有帮助

于 2018-09-23T02:25:00.150 回答
0

尝试以下,

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

或者

在 Search Text Box Changed 事件中,尝试以下操作,

(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", searchTextBox.Text);
于 2013-10-09T06:55:29.130 回答
0

这个主题有两种方法:

1 如果您使用的是数据表

bsItems.Filter = "VendorAccount like'*" + txtFilterAccount.Text + "*'";

2 使用像 dapper 这样的 ORM,您必须为存储的过滤数据创建新列表

lstFilteredVendors = lstVendors.Where(x => x.VendorAccount.Contains(t.Text)).ToList();
                    bsItems.DataSource = lstFilteredVendors;
于 2019-11-06T03:55:25.270 回答
0
        for (int i = 0; i < dataGridView1.Rows.Count; i++) 
        {
            if (dataGridView1.Rows[i].Cells[6].Value.ToString().Contains(textBox1.Text))
            {
                dataGridView1.Rows[i].Visible = true;
            }
            else if (dataGridView1.Rows[i].Cells[0].Value.ToString().Contains(textBox1.Text))
            {
                dataGridView1.Rows[i].Visible = true;
            }
            else if (dataGridView1.Rows[i].Cells[2].Value.ToString().Contains(textBox1.Text))
            {
                dataGridView1.Rows[i].Visible = true;
            }
            else
            {
                dataGridView1.Rows[i].Visible = false;
            }
        }
于 2021-06-11T07:50:00.667 回答