-1

I am trying to make a column in WPF datagrid to display currency in the following format:

value <= 9999, display format set to $1,234;
value <= 99999, display format set to $12k;
value >99999, display format set to $12M;

In addition, I would need to have the function that user can see the actual value and edit the value upon mouse click.

I have been trying to get the concept of IValueConverter, but couldn't get a working code.

Can someone help?

Update: with Ramin's answer, it is partially working. Here are the code:

public class DollarConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double v = double.Parse(value.ToString());

        if (v <= 9999)
        {
            return v.ToString("C0");
        }
        else if (v <= 999999)
        {
            double vv = v / 1000;
            return vv.ToString("C0") + "K";
        }
        else if (v > 99999)
        {
            double vvv = v / 1000000;
            return vvv.ToString("C0") + "M";
        }

        return v;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

in WPF:Binding="{Binding LBRRev, Converter={StaticResource DollarConverter}}"

OptimusPrime
  • 191
  • 1
  • 11

2 Answers2

0

This is not the exact solution, but can help you solve it:

public class StringFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int v = int.Parse(value.ToString());

        if (v <= 9999)
            return v.ToString("C0");
        else if (v <= 99999)
            decimal vv = v / 1000;
            return vv.ToString("C0") + "k"; 
        return v;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Use it in the Binding:

 <DataGridTextColumn  Binding="{Binding Converter={StaticResource StringFormatConverter }}"/>
rmojab63
  • 3,513
  • 1
  • 15
  • 28
0

After a bit of research, I have a working version now. The converter:

public class DollarConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {            
        double v = double.Parse(value.ToString());

        if (v <= 9999)
        {
            return v.ToString("C0");
        }
        else if (v <= 999999)
        {
            return (v/1000).ToString("C0")+"K";
        }
        else if (v > 999999)
        {
            return (v/1000000).ToString("C1") + "M";
        }


        return v;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {

        throw new NotImplementedException();
    }
}

In the xaml:

<DataGridTemplateColumn Header="Mat. Rev" Width = "22*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding MatRev, Converter={StaticResource DollarConverter}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=MatRev, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn> 

Hope this helps anyone who have the same question.

OptimusPrime
  • 191
  • 1
  • 11