Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format values in a Datagrid

Tags:

c#

wpf

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;
like image 957
Kyle Avatar asked Nov 28 '10 21:11

Kyle


People also ask

How do I format cell values in a datagridview control?

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.

What is the use of cellformatting event in datagridview?

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.

What is the use of datagridview?

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.

What is the datagridviewcellstyle class used for?

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.


2 Answers

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 output screenshot

for anything other than currency find stringFormat specifier here

like image 98
JD-V Avatar answered Oct 18 '22 22:10

JD-V


Use the StringFormat property:

<DataGridTextColumn Binding="{Binding Path=Date, StringFormat=d}" Header="Date" />
<DataGridTextColumn Binding="{Binding Path=Amount, StringFormat=C}" Header="Amount" />
  • Standard Numeric Format Strings
  • Standard Date and Time Format Strings
like image 37
SLaks Avatar answered Oct 19 '22 00:10

SLaks