简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM