Binding Source doesn't work on column names wi

2019-07-20 10:27发布

问题:

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.