0

I've got some Columns in a DataGridView that have their Binding property set to something like the following:

Binding="{Binding NetPrice}" 

The problem is that this NetPrice field is a Decimal type and I would like to convert it to Double inside the DataGrid.

Is there some way to do this?

1
  • That doesn't work as-is? Commented Oct 20, 2009 at 21:47

1 Answer 1

1

I would create a Converter. A converter takes one variable and "converts" it to another.

There are a lot of resources for creating converters. They are also easy to implement in c# and use in xaml.

Your converter could look similar to this:

public class DecimalToDoubleConverter : IValueConverter { public object Convert( object value, Type targetType, object parameter, CultureInfo culture) { decimal visibility = (decimal)value; return (double)visibility; } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException("I'm really not here"); } } 

Once you've created your converter, you will need to tell your xaml file to include it like this:

in your namespaces (at the very top of your xaml), include it like so:

xmlns:converters="clr-namespace:ClassLibraryName;assembly=ClassLibraryName" 

Then declare a static resource, like so:

<Grid.Resources> <converters:DecimalToDoubleConverter x:Key="DecimalToDoubleConverter" /> </Grid.Resources> 

Then add it to your binding like this:

Binding ="{Binding Path=NetPrice, Converter={StaticResource DecimalToDoubleConverter}" 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I've used Converters before, I just thought that maybe there was an alternative with some of the simpler data types.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.