[英]Binding Source doesn't work on column names with two or more words in DataGridView C#
I am using BindingSource.Filter to filter data on my datagridview. 我正在使用BindingSource.Filter来过滤datagridview上的数据。 I used the following code:
我使用以下代码:
BindingSource bs = new BindingSource();
bs.DataSource = datagridview1.DataSource;
bs.Filter = "columnName like '%" + textBox1.Text + "%'";
datagridview1.DataSource = bs;
This code works. 此代码有效。 But when I filter data on a two-word column, the code does not work anymore.
但是,当我在两字的列上过滤数据时,代码不再起作用。 I tried putting apostrophe on those words like
'column name' like '%" + tbFilter.Text + "%'
, but this does not help. 我尝试在
'column name' like '%" + tbFilter.Text + "%'
类的'column name' like '%" + tbFilter.Text + "%'
单词上加上撇号,但这无济于事。 Please help me find the right code to filter data on my columns. 请帮助我找到正确的代码以过滤列中的数据。
Enclose column name in []
: 将列名称括在
[]
:
bs.Filter = "[column Name] like '%" + textBox1.Text + "%'";
I think it's always a good idea even if your column names are one-word. 我认为,即使您的列名是一个单词,这也是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.