I have looked around but I can't find a solution that either works or specifically answers my question. I have a simple datagrid setup to display data from a List<T>. One of the columns is a currency value stored as a Decimal and one is a DateTime object.
The date looks ok but I want to drop the time from the end of it. The decimal is just a decimal. I want it to show comma's for every 3 digits and I only want two decimal places (it currently shows about 8 I think).
I have tried a number of approaches including setting the Coverter however while the coverter code is being called and is converting values (I checked) the column is empty whenever I use this.
It seems to me this should be a lot simpler, a simple StringFormat parameter perhaps? What am I missing here that is making this so difficult?
Here is the xaml:
<igDP:XamDataGrid x:Name="RepTakeDataGrid" DataSource="{Binding RepTakeDataSource}" Theme="Office2k7Blue" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False" HighlightAlternateRecords="True" AllowAddNew="False" AllowDelete="False" /> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout > <igDP:FieldLayout.FieldSettings> <igDP:FieldSettings AllowSummaries="True" CellClickAction="SelectRecord" ExpandableFieldRecordHeaderDisplayMode="DisplayHeaderOnlyWhenExpanded" /> </igDP:FieldLayout.FieldSettings> <igDP:FieldLayout.Fields> <igDP:Field Name="Amount" Label="Take Amount ($)" Converter="{StaticResource formatConvertor}" ConverterParameter="'{0:C2}'" /> <igDP:Field Name="EntryAccount" Label="Entry Account"/> <igDP:Field Name="SplitAccount" Label="Split Account" /> <igDP:Field Name="RepNumber" Label="Rep #" /> <igDP:Field Name="RepName" Label="Rep Name" /> <igDP:Field Name="DateReceived" Label="Date" /> <igDP:Field Name="RepCodeString" Label="Rep Code" /> <igDP:Field Name="ClientName" Label="Client" /> <igDP:Field Name="PolicyNumber" Label="Policy #" /> <igDP:Field Name="EntryType" Label="Type" /> </igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid>
Also if you notice anything else wrong with this approach please let me know.
Thanks,
Chris
Thanks, I will have to try that out. It is likely that I will be getting the full version of the controls package as I have use for the controls in a few situations including ASP.NET and Winforms, but it is nice to be able to have a working solution for number formatting in the meantime.
Hello Chris,
As I suspected there is a way to do the same thing with only the XamTextEditor:
<Style TargetType="{x:Type igEditors:XamTextEditor}" x:Key="TheDateStyle"> <Setter Property="Format" Value="dd/MMMM/yyyy" /></Style><Style TargetType="{x:Type igEditors:XamTextEditor}" x:Key="TheCurrencyStyle"> <Setter Property="Format" Value="$###,#.##" /></Style>
And you set them to the field int the xamDataGrid like this:
<igDP:Field Name="Money"> <igDP:Field.Settings> <igDP:FieldSettings EditAsType="{x:Type sys:String}"" EditorType="{x:Type igEditors:XamTextEditor}" EditorStyle="{StaticResource TheCurrencyStyle}"/> </igDP:Field.Settings></igDP:Field> <igDP:Field Name="LastPayed"> <igDP:Field.Settings> <igDP:FieldSettings EditAsType="{x:Type sys:String}" EditorType="{x:Type igEditors:XamTextEditor}" EditorStyle="{StaticResource TheDateStyle}" /> </igDP:Field.Settings></igDP:Field>
Hope this helps.
Best regards Petar.
By default you do not create a readonly data grid, Of course you can set several properties like AllowEdit in the FieldSettings or AllowDelete in the FieldLayoutSettings. This though does not make it "read only", it only prevents the user form editing the data during runtime and does not prevent setting properties like Styles...
About the Converters... they are intended to be use for converting data from one type to another e.x. decimal to string or decimal to int thedo not format their appearance.
I' am currently working on a way to solve your problem and I'll write as soon as possible.
Ok just to update, I have downloaded the full evaluation package and I can confirm this works. My questions about the solution still stand though and I would like to hear what you have to say about it. I found the approach to common string formatting a bit odd, when you compare it to the stringformat options for databinding on say a textblock or even using the CTP WPF Datagrid on codeplex (which obviously has it's own shortcommings and issues)
I am overall fairly impressed with the controls you provide for WPF and I am currently evaluating which (if any) controls package my office will want to go with for development.
On a related note regarding the express version. While it is only a free version, I find the fact that it has such powerful features like grouping by column and hierarchical databinding (probably the best out of any available datagrid) at odds with the fact that you can't format a number or a date. It does seem like an obvious oversight.
Sorry, I probably should have explained I am using the express edition of the xamDataGrid, which does not appear include these editors, only the text editor.
Is it just not possible to format the text with the express edition? I can understand the express edition have limitations, but this is overall a very simple datagrid display.
Since I am using A M-V-VM here I can obviously format in the viewmodel and bind to a string property instead. My main reason for not doing this is loosing the ability to use the column summaries and get sums of the decimal amounts (I am assuming it does not format strings).
The last thing I can think of would be to write something that inherits from Decimal but overrides the ToString() method, though I am not sure if that will have the intended effect.