使用文本框过滤datagridview [英] Use textbox to filter datagridview
本文介绍了使用文本框过滤datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写代码,以便当用户在文本框中键入单词时,相应的行是数据库视图中唯一剩下的一行...
但是当我输入一个单词时,表格变成空白..
I am trying to write a code so that when the user types a word into the textbox the corresponding row is the only one left in the databseview...
But when I type the a word, the tabel goes blank..
private void SearchTxtBx_TextChanged(object sender, EventArgs e)
{
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from [Product Inventory] Where [Product Name] = '" + SearchTxtBx.Text + "'", sqlcon);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
sqlcon.Close();
我的尝试:
What I have tried:
private void SearchTxtBx_KeyPress(object sender, KeyPressEventArgs e)
{
var cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource, dataGridView1.DataMember];
var dataView = (DataView)cm.List;
dataView.RowFilter = string.Format("Name='{0}'", SearchTxtBx.Text);
}
推荐答案
尝试LIKE而不是等于:
Try LIKE instead of equals:
SELECT * FROM [Product Inventory] WHERE [Product Name] LIKE '%Search words%'
'%'字符充当通配符与文件搜索中的*相同。
The '%' character acts as a wildcard in the same way '*' does in file searches.
这篇关于使用文本框过滤datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文