Paszt said:
If you were to use the method cjard shows above, the columns property is found in the DataTable stored in the DataSet. For eample MyDataTableDataSet.DataTableName.Columns. DataTableName will be the name of your dataTable in MyDataTableDataSet.
One problem with the method is the line Dim filterStr as String = "{0} = '' ".
If the user typed d in the textbox you will receive an error because the start of the filter string will look like: "[d] = ''" and unless you have a column named d, that column won't be found.
Apologies, i have indeed made a slight syntax error. It should read:
Dim filterStr as String = "'{0}' = '' "
Note the extra ' ' around the {0}. When the user types nothing into the box, the resulting Filter string will start with:
'' = ''
which logically evaluates to true, so all rows are shown. THis may or may not be a requirement, but without this section present, an empty textbox will cause all rows to be filtered.
Another problem is if you have any columns contaning non-string type data, for example Integer or Boolean types because you can't use the LIKE comparer with those data types.
again, a good point. The filter string supports a very limited command set which it seems to share with the Expression property of a datacolumn. Hence, the available commands you have to use in filter are
CONVERT, ISNULL, LEN, SUBSTRING, IIF and TRIM in addition to the regualar maths operators.
FOr more info, see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatacolumnclassexpressiontopic.asp
Note it might also be the case that * is used as a wildcard instead of/aswell as %
Hence to solve this problem you can detect the type of the column when you are building the string and either:
Change the criteria so you arent using LIKE
Or change the composition of the format string using CONVERT or similar
Of the two, i prefer the former. You may additionally choose to skip searching of date columns if they are formatted differently to the default. If the user sees 10 Jul 2006, they may not realise that to search for a date they must use #07/10/2006#
You can provide a date time picker in this case
Here is a guide to performing criteria change so we arent using like:
[LEFT]Dim filterStr as String = "{0} = '' "
For Each col as DataColumn in MyDataTable.Columns
Select Case col.GetType().ToString()
Case "System.String"
filterStr &= " OR " & col.ColumnName & " LIKE '{0}%'"
Case "System.Int32"
filterStr &= " OR " & col.COlumnName & " = {0}"
End Select
Next col[/LEFT]
I'm not in a great position to test this, but maybe you are.. You can place a textbox on your form, and upon textchanged, Try set the filter to the text. Catch any exceptions and set the filter to nothing if an exception arises. use a reasonably small number of rows for this as using thousands may slow your testing.
As paszzssszt says, youre probably better off just writing your filter string out if there is a low number of columns..
filterStr = "'{0}' = '' OR strCol LIKE '{0}%' OR numCol = {0} OR ..."