So you've got an integer field that you want your users to be able to search on but lightswitch won't allow it. You could do something like Rich Dudley suggests here. Rich's method is awesome and it works but if you're like me and you don't like the idea of having a search box at the top of the grid and another search box above it then you can do this....
First, go to the table that has the integer you want to search on :
My field is called 'ProductCode'. Next create a new field of a String type. Call it whatever your field is named with 'String' after it. Mine is 'ProductCodeString' but yours will most likely be different. :-)
Then tick the 'Is Searchable' box.
Next we need to go back to the integer field that we want to search on and click on it:
Then click on Write Code and choose the _Changed method for the field you clicked on (ProductCode_Changed in my case).
In the code for the method use the following:
Partial void ProductCode_Changed()
{
this.ProductCodeString = this.ProductCode.ToString();
}
So now whenever the ProductCode field is changed LightSwitch will save a copy of the field as a string in another field so that you can search on it. That's fine and dandy if you're starting from scratch but if you've already got a a lot of data in your database then you'll want to use a query to update the ProductCodeString field with the ProductCode as a string. You can do that with a query similar to this one:
UPDATE Products SET ProductCodeString = CAST(ProductCode AS nvarchar(255))
But of course personalize the field and table names.
This is a bit of a stinky hack to be honest. It intentionally creates database bloat and the only real function of the information is to provide searchability - is is completely useless in all other contexts. So think carefully before you use this - if you've got billions of records on the table in question then creating an extra string field for every record might not be a good idea.....
I've used a very similar method to make fields that are relationships sortable as well. If anyone wants me to explain that in detail then please comment.
This code is taken from my LightSwitch Star Contest Entry. Have a look if you're interested!



The C# method didnt work and I'm not gonna do the Sql one so i came up with my own way. Try creating a view in sql.
ReplyDeleteCreate view as vw_test
as
select id,cast(id as varchar(100)) idString,etc...
from table_test
then in a lightswitch screen click add data item,select local property, then select [database name]data.vw_test(table)
then drag that thing(called property1 by default) you just added on to top of the screen and make sure it is an auto complete box
then edit table_test query add filter
id = a param called id
select the param id properties and type in parameter binding: vw_test.id
then drag table_test under the property1
and run it