Select to view content in your preferred language

Query datagrid with date field

930
3
08-12-2011 09:14 AM
DorothyMortenson
Deactivated User
Hello,
I have a query that provides the results in a datagrid.  All looks swell, except I have one field that is a date, in which I get the time (It's not there in the attribute table in ArcMap) 😕

I'm not sure I am understanding how to get the attribute over to the datagrid formatted as I would prefer - either as a shortdate or even as a trimmed string.  In fact, in my testing I haven't even been able to pass over any custom text (say: string newvalue = "something";  ) I'm definately missing something.

I'm using silverlight 4; ArcGIS Server 9.3.1

Any suggestions?
Dorothy
0 Kudos
3 Replies
JenniferNery
Esri Regular Contributor
You can use StringFormat property, here's an example of how it is used: http://mattduffield.wordpress.com/2011/02/14/data-binding-and-stringformat-in-silverlight/
0 Kudos
DorothyMortenson
Deactivated User
Well, it's been interesting...

It appears that if you have a date field in your geodatabase, it returns as a string - with the time - in a query. 

I have added a converter class as described by:
http://blogs.msdn.com/b/scmorris/archive/2008/03/27/defining-columns-for-a-silverlight-datagrid.aspx

I was able to get the field to look correct with a short date (1/3/2011). However, it does not sort correctly (sort by year).

I changed my presentation of it in the converter class to be YYYY-MM-DD, However, the DataGridTextColumn is still sorting on the original field, which is a date as a string, so all the 1/ are together, dispite the day or year.

Part II:
I did find an easier way to present the date field in a short date:
<sdk:DataGridTextColumn Binding="{Binding Attributes[priority_date],StringFormat=\{0:d\}}" Header="Priority Date"  CanUserSort="True" />

However, this only works if the field was truly a date field. But since the query is returning a string, it cannot be formatted in this way.

Still working on a solution...
0 Kudos
DorothyMortenson
Deactivated User
As it turns out, you cannot sort on number fields either.

I ended up creating my own class that defines all of the fields as the data type they are suppose to be.  I am then able to do with them as I wish.  Dates sort as they should (by year, month, day instead of by month). 

If you put a stop in your code just after the query finishes and look at the results, you will see that the results are a dictionary with either strings or numbers. It doesn't maintain the data types even as they are defined in the web service.  Seems crazy to me.

After defining these fields, it does add just a little bit of time to process, which seems a bit needless. I'm hoping ESRI can fix this in a future version.

Dorothy
0 Kudos