Specify Guids for a RowFilter property of a DataView object
As many of you are aware we can provide an expression to filter out records from a DataView to the RowFilter property of a DataView object. For a recent development that I was involved I wanted to provide a list of Guids as the expression with an 'IN' operator.
Ex:
SomeDataTable.DefaultView.RowFilter = "customerid IN (" + guidString +")"; // The guidString is the string variable which contains the list of Guids.
( the field customerid is of Type Guid )
First I created the guidString as "{7F901912-DD67-47B9-A5B3-B702B9F84680},{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4},.......and so on"
But this did not work as I expected. It gave an error saying "Cannot interpret token '{' at position 16."
Then I created the string as "'{7F901912-DD67-47B9-A5B3-B702B9F84680}','{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4}',.......and so on"
This also did not work and gave an error saying "Cannot perform '=' operation on System.Guid and System.String."
Aftter that I found the solution from the MSDN itself.
I should format the guidString as "CONVERT('{7F901912-DD67-47B9-A5B3-B702B9F84680}','System.Guid'),CONVERT('{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4}', .... and so on"
Ex:
SomeDataTable.DefaultView.RowFilter = "customerid IN (CONVERT('{7F901912-DD67-47B9-A5B3-B702B9F84680}','System.Guid'),CONVERT('{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4}', .... and so on )"
Ex:
SomeDataTable.DefaultView.RowFilter = "customerid IN (" + guidString +")"; // The guidString is the string variable which contains the list of Guids.
( the field customerid is of Type Guid )
First I created the guidString as "{7F901912-DD67-47B9-A5B3-B702B9F84680},{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4},.......and so on"
But this did not work as I expected. It gave an error saying "Cannot interpret token '{' at position 16."
Then I created the string as "'{7F901912-DD67-47B9-A5B3-B702B9F84680}','{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4}',.......and so on"
This also did not work and gave an error saying "Cannot perform '=' operation on System.Guid and System.String."
Aftter that I found the solution from the MSDN itself.
I should format the guidString as "CONVERT('{7F901912-DD67-47B9-A5B3-B702B9F84680}','System.Guid'),CONVERT('{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4}', .... and so on"
Ex:
SomeDataTable.DefaultView.RowFilter = "customerid IN (CONVERT('{7F901912-DD67-47B9-A5B3-B702B9F84680}','System.Guid'),CONVERT('{85823FC5-1F2E-4034-B1CB-6DEE954AC4D4}', .... and so on )"
2 Comments:
Brilliant! Been struggling with this for quite a while now
We're converting our system from using int PKs to GUIDs and this was one of the last errors I was struggling with! Thanks for the help :)
Post a Comment
<< Home