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;
The following procedures demonstrate basic formatting of cell values using the DefaultCellStyle property of a DataGridView control and of specific columns in a control. For information about advanced data formatting, see How to: Customize Data Formatting in the Windows Forms DataGridView Control. Set the Format property of a DataGridViewCellStyle.
If a format string is in effect for the cell, it overrides your change of the Value property value unless you set the FormattingApplied property to true. The CellFormatting event is also useful when you want to set DataGridViewCellStyle properties for individual cells based on their values.
The DataGridView control provides automatic conversion between cell values and the data types that the parent columns display. Text box columns, for example, display string representations of date, time, number, and enumeration values, and convert user-entered string values to the types required by the data store.
The DataGridViewCellStyle class provides additional formatting properties for wordwrap, text alignment, and the custom display of null database values. For more information, see How to: Format Data in the Windows Forms DataGridView Control.
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
Use the StringFormat
property:
<DataGridTextColumn Binding="{Binding Path=Date, StringFormat=d}" Header="Date" />
<DataGridTextColumn Binding="{Binding Path=Amount, StringFormat=C}" Header="Amount" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With