Simon Grondin
Simon Grondin

Reputation: 79

Text formatting in Datagrid cell

I am trying to format numbers in datagrid data cells, in order to make '1000000.52' appear like : '1 000 000.52' for instance.

I have tried to look for properties that enable such things, and can only find the ContentStringFormat property inside the DataGridCell class. Unfortunately, what I tried do not work :

<DataGrid  CanUserAddRows="False" Name="EGrid" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=Data, Mode=TwoWay, ElementName=ExportableGrid}">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="ContentStringFormat" Value="## ### ###.00"/>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

How could I achieve this ? Any help would be very much appreciated, thanks in advance.

Upvotes: 0

Views: 1356

Answers (2)

Mac
Mac

Reputation: 957

I'd create a custom value converter:

    Imports System.Globalization

Public Class DecimalFormatConverter
    Implements IValueConverter

    Public Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        Dim decimalVal As Decimal = Nothing

        If value IsNot Nothing AndAlso parameter IsNot Nothing AndAlso Decimal.TryParse(value.ToString(), decimalVal) Then
            Return decimalVal.ToString(parameter.ToString())
        End If

        Return value
    End Function

    Public Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException
    End Function

End Class

To use it in xaml:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp6VB"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.Resources>
            <local:DecimalFormatConverter x:Key="DecimalFormatConverter" />
        </Grid.Resources>
        <DataGrid  CanUserAddRows="False" x:Name="EGrid" Grid.Column="0" ItemsSource="{Binding Nums}" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Number">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Converter={StaticResource DecimalFormatConverter}, ConverterParameter='## ### ###.00'}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Output:

enter image description here

Upvotes: 3

Simo
Simo

Reputation: 953

You need to convert it to string, split it and add spaces where needed.

I hardcoded your problem. If you want the snippet to work with different number, I suggest you to use the same logic and create a Select/Case on the lenght of the number.

This is the code to solve your problem:

    Dim number As Double = 1000000.52
    Dim str() As String = Convert.ToString(number).Split(",")
    str(0) = "" & _
        str(0).Substring(0, 1) & _
        " " & str(0).Substring(1, 3) & _
        " " & str(0).Substring(4, 3) & _
        "." & str(1)

Upvotes: 0

Related Questions