6 Replies Latest reply on Jun 6, 2008 11:11 AM by Garyl Woolworth

    Format Date in a DataGrid

      I have a dataset that is passed from my .net webservice to flex client and when i set the datagrid dataprovider to the dataset object that I pass, it copies date objects and displays in the long date format like "Thu Jun 5 00:00:00 GMT-0600 2008"

      And I am trying to figure out how to get the dateformatter to work. The code I added doesnt seem to format the date. Is there a way to display the date field as short date like "MM/DD/YYYY"?
        • 1. Re: Format Date in a DataGrid
          Kurt Mossman Adobe Employee
          You can use the labelFunction and the DateFormatter to handle this. The item is passed into the date formatter and it will configure the date as you like.
          • 2. Re: Format Date in a DataGrid
            Jihaes Level 1
            I'd understand if I had the datagrid set up like the way you did but the problem I have is I have a whole datatable that I passed into flex that I need to assign to the datagrid so it gets all the columns and rows. and i just want to change the format of the columns that are date type. (so i need to assign the datagrid from the script.
            in the code i meant dg.dataProvider = dataSet.Table.Rows (dataSet is the C# dataset object I pass into flex)

            How would that work with your solution?
            • 3. Re: Format Date in a DataGrid
              ntsiii Level 3
              Same way. Use a labelFunction for the columns that are dates.

              Or are you saying you are not declaring individual DataGridColumns?

              Pre-processing the data is an ok way to handle this. First you need to be sure you have the date in a format that the AS Date object understands. Is your if condition, "i is Date" ever returning true?

              What format are you getting?

              • 4. Re: Format Date in a DataGrid
                Garyl Woolworth
                Just a quick thought in the code attached you have for(var i:Object and that is what you are trying to format. However a DateFormatter doesn't know how to format an entire object. Wouldn't you either need to loop over all the identifiers in the object so formatter.format(i.DateColumn1) etc.. or scope i into the more specific field of what it is your trying to format? You are also scoping i to an Object so I wouldn't think your if(i is Date) is going to ever return true.
                • 5. Re: Format Date in a DataGrid
                  Kurt Mossman Adobe Employee
                  For performance reasons you'll want to handle this during the display and not modify the objects directly. Let's say you have 2,000 rows of data... you would end up formatting all 2,000 records. Your DataGrid in most cases will scroll and only about 20 records would be displayed at a time. When you use the labelFunction you're only changing 20 at a time and performance is faster. In addition you're able to modify the Date as a Date later if your requirements change.

                  I have attached an actionscript example for you to use. If you're creating the entire grid on the fly then you'll just need to add more DataGridColumn's and add them to the dg.columns array.
                  • 6. Re: Format Date in a DataGrid
                    Garyl Woolworth Level 1
                    Kurt is correct on the performance issue however my last comment on that is if you are returning a 2000 row recordset back to Flex I would do the processing on the server side returning back two identifiers one as the original date and the second as the formatted date eliminating the need for labelFunctions or DateFormatters all together. Better to chew up a servers CPU then it is your clients.