This is a migrated thread and some comments may be shown as answers.

[Solved] DataMemberBinding.Converter broken

1 Answer 250 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Patrick Holloway
Top achievements
Rank 1
Patrick Holloway asked on 23 Nov 2009, 06:12 PM
in Q2, this code worked fine, to format columnar data in $0.00 format:

GridViewDataColumn gvdc = (GridViewDataColumn)_dg.Columns[x];  
    if (gvdc.DataMemberBinding.Converter == null) {  
    gvdc.DataMemberBinding.Converter = new FloatToMoneyConverter();  
}  
 
 
public class FloatToMoneyConverter : IValueConverter {  
     
   public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {  
      double val = System.Convert.ToDouble(value);  
      string fval = String.Format("{0:C}", val);  
      return fval;  
}  
 
   public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {  
      return System.Convert.ToDouble(value);  
   }  
}  
 

Now, with Q3, the converter is only applied when the cell is in Edit mode. Worse, it throws an exception when leaving the cell, because ConvertBack() is trying to convert a string of $0.00 format to a float. It seems completely backwards from what Q2 did.

If this is not a bug, what would be the correct way to implement this?

Thanks.

EDIT: I opened a support case for this, #261228, and sent along a sample project. Thanks 

1 Answer, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 26 Nov 2009, 09:54 AM
Hi Patrick Holloway,

1. You need to specify the IValueConverters before binding the grid, in the CreateColumn method. But I do not think you will be needing them at all. See the next points.

2 & 3. With Q3 we have made several changes. If there is an IValueConverter the edit mode will take it into account. That is it will use the converted value which would have to be converted back when editing finishes.

This means that if you decide to stick to converters you will have to rewrite your ConvertBack method to receive a string such as "$0.00" and return the double 0. But we have a better suggestion. If you are using this converter with the sole purpose of displaying currency, the the proper way to do this would be to use the column's DataFormatString property like this:

col.DataFormatString = "{0:C}";

This will have two benefits -- in edit mode you will see pure raw numbers (without the currency sign) and you will not have to implement a ConvertBack method that parses currency strings.

I have modified and attached the project. It is using our latest internal build binaries.

I hope this helps. Let me know if there are other issues.

Best wishes,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Patrick Holloway
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or