Binding Source doesn't work on column names with two or more words in DataGridView C#

空扰寡人 提交于 2019-12-11 04:32:32

问题


I am using BindingSource.Filter to filter data on my 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. Please help me find the right code to filter data on my columns.


回答1:


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.



来源:https://stackoverflow.com/questions/29141082/binding-source-doesnt-work-on-column-names-with-two-or-more-words-in-datagridvi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!