I have a dataGridView control on a form.
I have a dataSet of a SQL database created in the project.
I can easily bind a table to my dataGridView control, and the tableAdapter fills the dataGridView - no problem.
Now, I have been through every forum and FAQ but cannot, for the life of me, get the defaultView.rowFilter() function to work on the dataTable bound to the dataGridView.
I had it working perfectly once before with none of the complicated and confusing work-arounds that almost every other answer seems to give. In fact the filtering worked with this simple one-liner:
(dataGridCustomers.DataSource as DataTable).DefaultView.RowFilter = String.Format("'{0}' LIKE '{1}%' OR '{0}' LIKE '%{1}%' ", columnName, txtFilterTxt.Text);
I have tried all sorts of things, such as messing around with creating dataTables and bindingSources separate from the main dataSet that I am using throughout my project, but to no avail.
And as I have said - I have been through what feels like dozens of forums but cannot find the simple method that I had working before which is why I'm resorting to asking my own question which I rarely do.
I feel like it should be simple - what am I doing wrong? Any help would be much appreciated.
Thanks to Jimi for the suggestion of using a binding source (as well as removing the quotes around my filter query):
Why do you have Column names in quotes? -- I don't see how using a BindingSource as DataSource of your DGV makes things complicated. In that case, it's just
(dataGridCustomers.DataSource as BindingSource).Filter = "...";
. But you have extra tools to work with (and the DataSource of the BindindSource can be something else entirely, but you use the same methods)
You probably have removed the quotes also from the values. The filter should be
string.Format("{0} LIKE '{1}%' OR {0} LIKE '%{1}%' ", columnName, txtFilterTxt.Text);
. Or$"{columnName} LIKE '{txtFilterTxt.Text}%' OR {columnName} LIKE '%{txtFilterTxt.Text}%'";
Everything seems to be working fine now.