[英]How to filter datagridview using a textbox in C#?
I tired to filter a datagridview using a textbox, the textbox is contained in a tabpage, but it is not working, here is the code : 我厌倦了使用文本框过滤数据网格视图,文本框包含在标签页中,但它不起作用,这里是代码:
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "like '%" + textBox1.Text.Trim() + "%' ";
}
catch (Exception) { }
}
RowFilter
allows you to specify a filter based on column values . RowFilter
允许您根据列值指定过滤器。 So the LIKE
applies to a specific column, not to the whole row. 因此,
LIKE
适用于特定列,而不适用于整行。 So your condition should be 所以你的病情应该是
"YourColumn like '%" + textBox1.Text.Trim() + "%'
Also, don't forget that the TextBox could contain the '
character, so you need to escape it: 另外,不要忘记TextBox可以包含
'
字符,所以你需要转义它:
"YourColumn like '%" + textBox1.Text.Trim().Replace("'", "''") + "%'
Or, cleaner: 或者,清洁:
string.Format("YourColumn like '%{0}%'", textBox1.Text.Trim().Replace("'", "''"));
try this it is searching for the letter no matter where is that letter ( beginning, mid, or at the end) 试试这个它正在寻找这封信,无论那封信是在哪里(开始,中间或结尾)
private void TextBox1_TextChanged(object sender, EventArgs e)
{
try
{
//this code is used to search Name on the basis of TextBox1.text
((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = string.Format("Column_Name like '%{0}%'", TextBox1.Text.Trim().Replace("'", "''"));
}
catch (Exception)
{
}
}
This one searching from the first letter with following the next one by one. 这个从第一个字母开始搜索,然后逐个跟随下一个字母。
try
{
((DataTable)dataGridViewX1.DataSource).DefaultView.RowFilter = "Column_Name like'" + textBox1.Text.Trim().Replace("'", "''") + "%'";
}
catch (Exception)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.