在datagridview c#中过滤数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19265056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter data in datagridview c#
提问by user2837847
i am trying to filter out the data that i want, and hide all the other data instead. Here is my list of code,
我试图过滤掉我想要的数据,并隐藏所有其他数据。这是我的代码列表,
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;
}
but when i run it, it prompts me error message
但是当我运行它时,它提示我错误消息
"Missing operand after 'ID' operator."
“'ID' 运算符后缺少操作数。”
and i have tried this too, same error message.
我也试过这个,同样的错误信息。
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;
}
can anyone please help me clarify the problem? thanks.
任何人都可以帮我澄清问题吗?谢谢。
采纳答案by Rajesh Subramanian
Try with following ,
尝试以下,
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "yourColumnName like '%" + textBox1.Text + "%'";
dataGridView1.DataSource = bs;
or
或者
In Search Text Box Changed event, try the following,
在搜索文本框更改事件中,尝试以下操作,
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", searchTextBox.Text);
回答by cesc
Try with following
尝试以下
BindingSource bs = new BindingSource();
bs.DataSource = dataGridView1.DataSource;
bs.Filter = "[HeaderText] Like '%" + searchTextBox.Text + "%'";
dataGridView1.DataSource = bs;
回答by Jpabs
I set my datasource as list and used LINQ to filter the datagridview.
我将我的数据源设置为列表并使用 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;
}
I hope it helps
我希望它有帮助
回答by Mohamed Sadat
there is 2 approach in this subject:
这个主题有两种方法:
1 If you are using data table
1 如果您使用的是数据表
bsItems.Filter = "VendorAccount like'*" + txtFilterAccount.Text + "*'";
2 Using ORM like dapper you will have to create new list to stored filtered data
2 使用像 dapper 这样的 ORM,您必须创建新列表来存储过滤数据
lstFilteredVendors = lstVendors.Where(x => x.VendorAccount.Contains(t.Text)).ToList();
bsItems.DataSource = lstFilteredVendors;