Reputation: 4366
Is there any way to format the values that are bound to a datagrid? For example I have the following:
<DataGrid AutoGenerateColumns="False" Height="487" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dgTransactionLog" VerticalAlignment="Top" Width="404">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Date}" Header="Date" />
<DataGridTextColumn Binding="{Binding Path=Payee1.Name}" Header="To/From" />
<DataGridTextColumn Binding="{Binding Path=Amount}" Header="Amount" />
</DataGrid.Columns>
</DataGrid>
I'd like the Date column to be just date (not time) and the Amount column to be currency format. Here's how I populate the datagrid:
var transactions = TransactionManager.GetTransactions();
dgTransactionLog.ItemsSource = transactions;
Upvotes: 15
Views: 21738
Reputation: 302
If the stringformat is variable, the above solutions will not work.
<DataGrid.Resources>
<DataTemplate x:Key="YTemplate">
<Label Content="{Binding Y, Mode=OneWay}" ContentStringFormat="{Binding StringFormat, ElementName=Parent_UC}"/>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Y" Width="*" CellTemplate="{StaticResource YTemplate}"/>
</DataGrid.Columns>
Upvotes: 0
Reputation: 3666
One easiest way. here in the code below use your language code as a value of ConverterCulture. you can find your language code here
<DataGridTextColumn Binding="{Binding Profit, ConverterCulture='gu-IN' ,StringFormat=c}" Header="Profit" Width="*" MinWidth="80" FontWeight="Normal"/>
the output will be in your local currency
for anything other than currency find stringFormat specifier here
Upvotes: 7
Reputation: 887453
Use the StringFormat
property:
<DataGridTextColumn Binding="{Binding Path=Date, StringFormat=d}" Header="Date" />
<DataGridTextColumn Binding="{Binding Path=Amount, StringFormat=C}" Header="Amount" />
Upvotes: 32